0

I have two files, and I need to compare the second field from File1 and the first field from File2. If there is a match to print the second field of File2 and the entire line matched from File1 If there is no match to print "NOT FOUND" and the entire Line from File1

File1

\\FILESERV04\PCO;S:\CA\USII ECOM;/FS7_434D/FILESERV04/BUSII;;;;\\FILESERV04\PCO\;467,390,611 Bytes;11,225 ;157 
\\FILESERV12\MINE$;S:\CA\Naka;/FS3_434D/FILESERV12/NAKA;;;;\\FILESERV12\MINE$\;0 Bytes;0 ;0 
\\FILESERV12\INTEG$;S:\CA\PLOTA;/FS3_434D/FILESERV12/INTEG;;;;\\FILESERV12\INTEG$\;231,094,432,158 Bytes;175,180 ;21,309 
\\FILESERV15\ED$;S:\CA\ED;/FS3_434D/FILESERV12/ED;;;;\\FILESERV15\ED$\;244,594,432,158 Bytes;145,040 ;21,311

File2

S:\CA\USII ECOM;782
S:\CA\PLOTA;0
S:\CA\Naka;781

Desired output:

782;\\FILESERV04\PCO;S:\CA\USII ECOM;/FS7_434D/FILESERV04/BUSII;;;;\\FILESERV04\PCO\;467,390,611 Bytes;11,225 ;157 
781;\\FILESERV12\MINE$;S:\CA\Naka;/FS3_434D/FILESERV12/NAKA;;;;\\FILESERV12\MINE$\;0 Bytes;0 ;0 
0;\\FILESERV12\INTEG$;S:\CA\PLOTA;/FS3_434D/FILESERV12/INTEG;;;;\\FILESERV12\INTEG$\;231,094,432,158 Bytes;175,180 ;21,309 
NOT FOUND;\\FILESERV15\ED$;S:\CA\ED;/FS3_434D/FILESERV12/ED;;;;\\FILESERV15\ED$\;244,594,432,158 Bytes;145,040 ;21,311

If the field number to compare is the same field number on both files this line works:

awk -F";" 'NR==FNR{a[$1]=$2;next}{if (a[$1])print a[$1]";"$0;else print "Not Found"";" $0;}' File1 File2

But is not working here because in this case I have different field number to compare from both files.

Thanks

Eduardo
  • 397
  • 2
  • 6
  • 19
  • possible duplicate of [awk Compare 2 files, print match and difference](http://stackoverflow.com/questions/13116353/awk-compare-2-files-print-match-and-difference) – Vijay Oct 29 '12 at 07:19

2 Answers2

4

One way using GNU awk:

awk 'BEGIN { OFS=FS=";" } FNR==NR { array[$1]=$2; next } { print ($2 in array ? array[$2] : "Not Found"), $0 }' File2 File1

Results:

782;\\FILESERV04\PCO;S:\CA\USII ECOM;/FS7_434D/FILESERV04/BUSII;;;;\\FILESERV04\PCO\;467,390,611 Bytes;11,225 ;157 
781;\\FILESERV12\MINE$;S:\CA\Naka;/FS3_434D/FILESERV12/NAKA;;;;\\FILESERV12\MINE$\;0 Bytes;0 ;0 
0;\\FILESERV12\INTEG$;S:\CA\PLOTA;/FS3_434D/FILESERV12/INTEG;;;;\\FILESERV12\INTEG$\;231,094,432,158 Bytes;175,180 ;21,309 
Not Found;\\FILESERV15\ED$;S:\CA\ED;/FS3_434D/FILESERV12/ED;;;;\\FILESERV15\ED$\;244,594,432,158 Bytes;145,040 ;21,311
Steve
  • 51,466
  • 13
  • 89
  • 103
  • why do you explicitly print the ORS? `print` would be simpler – glenn jackman Oct 29 '12 at 11:09
  • @steve: You could also remove the explicit FS and redundant parts of the print and make it print ($2 in array ? array[$2] : "Not Found"), $0. You'd need FS=OFS=";" in BEGIN instead of -F";" for that, but that's probably appropriate. Otherwise: print ($2 in array ? array[$2] : "Not Found") FS $0 – Ed Morton Oct 29 '12 at 14:33
3
 awk -F";" 'NR==FNR{a[$1]=$2;next}{if ($2 in a)print a[$2]";"$0;else print "Not Found"";" $0;}'  File2 File1
Guru
  • 16,456
  • 2
  • 33
  • 46