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

五迷三道 提交于 2019-12-20 06:27:32

问题


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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/13117346/awk-compare-2-files-2-fields-different-order-in-the-file-print-or-merge-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!