Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

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

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(i=1;i<=NF;i++)a[NR,i]=$i;next}{for(i=1;i<=NF;i++)if(a[FNR,i]!=$i)print $i"#"a[FNR,i]}' file1 file2 >output

But I want to print the whole line. How can I achieve that??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would say:

awk 'BEGIN{FS=OFS="|"}
     FNR==NR {for (i=1;i<=NF;i+=2) a[FNR,i]=$i; next}
     {for (i=1; i<=NF; i+=2)
         if (a[FNR,i] && a[FNR,i]!=$i)
             $i=$i"#"a[FNR,i]
     }1' f1 f2

This stores the file1 in a matrix a[line number, column]. Then, it compares its values with its correspondence in file2.

Note I am using the field separator | instead of || and looping in steps of two to use the proper data. This is because I for example did gawk -F'||' '{print NF}' f1 and got just 1, meaning that FS wasn't well understood. Will be grateful if someone points the error here!

Test

$ awk 'BEGIN{FS=OFS="|"} FNR==NR {for (i=1;i<=NF;i+=2) a[FNR,i]=$i; next} {for (i=1; i<=NF; i+=2) if (a[FNR,i] && a[FNR,i]!=$i) $i=$i"#"a[FNR,i]}1' f1 f2
a||d||f||b#a
1||1#2||3||4
1||2||r||f

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...