gawk

SED or AWK replace all with patterns from another file

徘徊边缘 提交于 2019-12-18 03:37:13
问题 I am trying to do pattern replacement using SED script but its not working properly sample_content.txt 288Y2RZDBPX1000000001dhana JP2F64EI1000000002d EU9V3IXI1000000003dfg1000000001dfdfds XATSSSSFOO4dhanaUXIBB7TF71000000004adf 10Q1W4ZEAV18LXNPSPGRTTIDHBN1000000005egw patterns.txt 1000000001 9000000003 1000000002 2000000001 1000000003 3000000001 1000000004 4000000001 1000000005 5000000001 Expected output 288Y2RZDBPX9000000003dhana JP2F64EI2000000001d EU9V3IXI3000000001dfg9000000003dfdfds

Parsing a CSV file using gawk

删除回忆录丶 提交于 2019-12-17 06:53:37
问题 How do you parse a CSV file using gawk? Simply setting FS="," is not enough, as a quoted field with a comma inside will be treated as multiple fields. Example using FS="," which does not work: file contents: one,two,"three, four",five "six, seven",eight,"nine" gawk script: BEGIN { FS="," } { for (i=1; i<=NF; i++) printf "field #%d: %s\n", i, $(i) printf "---------------------------\n" } bad output: field #1: one field #2: two field #3: "three field #4: four" field #5: five -------------------

awk if statement with simple math

送分小仙女□ 提交于 2019-12-14 02:03:23
问题 I'm just trying to do some basic calculations on a CSV file. Data: 31590,Foo,70 28327,Bar,291 25155,Baz,583 24179,Food,694 28670,Spaz,67 22190,bawk,4431 29584,alfred,142 27698,brian,379 24372,peter,22 25064,weinberger,8 Here's my simple awk script: #!/usr/local/bin/gawk -f BEGIN { FPAT="([^,]*)|(\"[^\"]+\")"; OFS=","; OFMT="%.2f"; } NR > 1 END { if ($3>1336) $4=$3*0.03; if ($3<1336) $4=$3*0.05;}1** Wrong output: 31590,Foo,70 28327,Bar,291 28327,Bar,291 25155,Baz,583 25155,Baz,583 24179,Food

command line pivot

限于喜欢 提交于 2019-12-14 01:18:31
问题 I've been hunting around the past few days for a set of command line tools, a perl or an awk script that allow me to very quickly transpose the following data: Row|Col|Val 1|A|foo 1|B|bar 1|C|I have a real 2|A|bad 2|C|hangover into this: A|B|C foo|bar|I have a real bad||hangover Note that there is only one value in the dataset for each "cell" (i.e., as with a spreadsheet, there aren't any duplicates of Row "1" Col "A") I've tried various awk shell implementations for transposing data - but

awk to compare two file by identifier & output in a specific format

╄→гoц情女王★ 提交于 2019-12-13 23:44:52
问题 I have 2 large files i need to compare all pipe delimited file 1 a||d||f||a 1||2||3||4 file 2 a||d||f||a 1||1||3||4 1||2||r||f Now I want to compare the files & print accordingly such as if any update found in file 2 will be printed as updated_value#oldvalue & any new line added to file 2 will also be updated accordingly. So the desired output is: (only the updated & new data) 1||1#2||3||4 1||2||r||f what I have tried so far is to get the separated changed values: awk -F '[||]+' 'NR==FNR{for

awk - how two scripts interact with each other?

岁酱吖の 提交于 2019-12-13 19:15:51
问题 I am not finding any clear tutorial on this topic. Say I have an input file as: 1 abc 1 def 1 ghi 1 lalala 1 heyhey 2 ahb 2 bbh 3 chch 3 chchch 3 oiohho 3 nonon 3 halal 3 whatever Say I would like to find the maximum number of column one appeared first, which is "3" that appeared 6 times. Then i will need to feed this number (i.e. 6) to another script to go through the file to do some computations. What are the ways to do this? Basically, i wonder if it's possible to write a function to go

Closest value different files, with different number of lines and other conditions ( bash awk other)

随声附和 提交于 2019-12-13 16:35:16
问题 I have to revive and old question with a modification for long files. I have the age of two stars in two files (File1 and File2). The column of the age of the stars is $1 and the rest of the columns up to $13 are information that I need to print at the end. I am trying to find an age in which the stars have the same age or the closest age. Since the files are too large (~25000 lines) I don't want to search in the whole array, for speed issues. Also, they could have a big difference in number

grep -Ff producing invalid output

五迷三道 提交于 2019-12-13 11:26:35
问题 I'm using code - grep -Ff list.txt C:/data/*.txt > found.txt but it keeps outputting invalid responses, lines don't contain the emails i input.. list.txt contains - email@email.com customer@email.com imadmin@gmail.com newcustomer@email.com helloworld@yes.com and so on.. email to match on each line, search files contain - user1:phonenumber1:email@email.com:last-active:recent user2:phonennumber2:customer@email.com:last-active:inactive user3:phonenumber3:blablarandom@bla.com:last-active:never

change the date of month in number format in awk [duplicate]

橙三吉。 提交于 2019-12-13 09:29:56
问题 This question already has answers here : Calculate date difference between $2,$3 from file in awk (2 answers) Closed 6 years ago . File 1 P1,06/Jul/2013,09/Jul/2013 P2,06/Jul/2013,10/Jul/2013 P3,06/Jul/2013,15/Jul/2013 Ouput i want like this: P1,06/07/2013,09/07/2013,3days P2,06/07/2013,10/07/2013,4days P3,06/07/2013,15/07/2013,9days some one help is need for this please 回答1: This answer is heavily dependent on BSD date formatting available on a mac #!/usr/bin/awk -f BEGIN { FS=" " } { split(

how to edit following line in sed, awk or anything else

限于喜欢 提交于 2019-12-13 09:03:27
问题 how to edit following line in sed, awk or anything else: root@laptop002:/tmp# cat /tmp/log 2016-03-01 06:08:26 {"id":"778640","cuid":"1","msid":"199033","lid":"582","Started":"1","qid":"9401"} batch is running to make it look like following: 2016-03-01 06:08:26 "msid":"199033" batch is running or 2016-03-01 06:08:26 msid is 199033 batch is running or 2016-03-01 06:08:26 msid=199033 batch is running 回答1: $ awk -F'[{,}]' '{print $1, $4, $NF}' file 2016-03-01 06:08:26 "msid":"199033" batch is