0

This question already has an answer here:

I have 2 files,

file1 ->

abc=3
abc=3
dfg=6
ggg=7

file2 ->

abc=3
dfg=4
sdf=9

I want the output in file3 as,

File2,File1,Result
abc=3,abc=3,Match
dfg=4,dfg=6,NoMatch
sdf=9,,NotPresent
,abc=3,Duplicate

I currently have a script,

grep -E 'abc|dfg|sdf' file1 >> file3
comm  <( sort -n file2 ) <( sort -n file3 ) |awk -F$'\t' 'BEGIN { OFS="," } $3 { print $3, $3, "MATCH"; next } { print $1, $2, "NO MATCH" }' > final_result.txt

The output looks like,

abc=3,abc=3,MATCH
,abc=3,NO MATCH
dfg=4,,NO MATCH
,dfg=6,NO MATCH
sdf=9,,NO MATCH

The output displays a blank if the value in file2 does not match the value in file1. For example, the only difference between dfg=4,,NO MATCH and ,dfg=6,NO MATCH is that the value has changed from 4 to 6. I would like the output to be

dfg=4,dfg=6,NoMatch 

instead of

dfg=4,,NO MATCH
,dfg=6,NO MATCH

And also,

abc=3

appears twice, but is displayed as

,abc=3,NO MATCH

I would like to have it as,

abc=3,,Duplicate

Any help is greatly appreciated.

marked as duplicate by Gilles shell-script Feb 7 '17 at 22:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • If your script does the job and all you want to do is to add HTML to the output, then just do so in your awk output. I don't see what's blocking you if you were able to do that script by yourself. – Julie Pelletier Feb 7 '17 at 5:12
  • @JuliePelletier : Edited my question. Sorry about the lack of clarity in the previous post. – akilesh raj Feb 7 '17 at 5:17
0
bash-4.1$ cat file1
abc=3
abc=3
dfg=6
ggg=7

bash-4.1$ cat file2
abc=3
dfg=4
sdf=9


bash-4.1$ awk 'NR==FNR{split($0,B,"=");Brr[B[1]]=$0;Arr[$0]++;next}($0 in Arr){print $0","$0",Match";next}split($0,C,"=")(C[1] in Brr){print Brr[C[1]]","$0",NoMatch"}END{for (i in Arr)if(Arr[i]>1){print i",,Duplicate"}}' file1 file2
abc=3,abc=3,Match
dfg=6,dfg=4,NoMatch
,sdf=9,NoMatch
abc=3,,Duplicate

Not the answer you're looking for? Browse other questions tagged or ask your own question.