Eduardo
Eduardo

Reputation: 397

awk compare 2 files, 2 fields different order in the file, print or merge match and non match lines

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

Upvotes: 0

Views: 3639

Answers (2)

Steve
Steve

Reputation: 54392

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

Upvotes: 4

Guru
Guru

Reputation: 16974

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

Upvotes: 3

Related Questions