Awk compares 2 files, 2 fields of different order in the file, print or match match and non-matching 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 mapped to 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 in 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 doesn't work here because in this case I have a different field number to compare against both files.

thank

0


source to share


2 answers


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

      



+2


source


One way 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

      

+4


source







All Articles