awk 2 files

Hello All

file1

XXX 801;ZZZZZZZZZZ;XXX 5830;WWWWW11; 
XXX 801;ZZZZZZZZZZ;XXX 5829;FFFF1111; 
XXX 212;DDDDDDDDD;XXX 5855;GGGG1111; 
XXX 212;DDDDDDDDD;XXX 5854;HHHH222; 
XXX 212;DDDDDDDDD;XXX 212;YYYY1111; 
XXX 212;DDDDDDDDD;XXX 5853;UUUU3333; 
XXX 2710;FFFFFFFFF;XXX 20010;JJJJJ33333; 
XXX 2710;FFFFFFFFF;XXX 2710;LLLL2222; 

file2

XXX 5853;1;QQQ5;UUUU3333;10752;NO 
XXX 2710;27;QQQ5;LLLL2222;10922;NO 
XXX 20010;28;QQQ5;JJJJJ33333;10922;NO 
XXX 212;2;QQQ5;YYYY1111;10752;NO 
XXX 5855;3;QQQ5;GGGG1111;10752;NO 
XXX 5854;4;QQQ5;HHHH222;10752;NO 
XXX 5830;6;QQQ5;WWWWW11;25600;NO 
XXX 5829;7;QQQ5;FFFF1111;25600;NO

output

XXX 801;ZZZZZZZZZZ;XXX 5830;WWWWW11;6;QQQ5;25600; 
XXX 801;ZZZZZZZZZZ;XXX 5829;FFFF1111;7;QQQ5;25600; 
XXX 212;DDDDDDDDD;XXX 5855;GGGG1111;3;QQQ5;10752; 
XXX 212;DDDDDDDDD;XXX 5854;HHHH222;4;QQQ5;10752; 
XXX 212;DDDDDDDDD;XXX 212;YYYY1111;2;QQQ5;10752; 
XXX 212;DDDDDDDDD;XXX 5853;UUUU3333;1;QQQ5;10752; 
XXX 2710;FFFFFFFFF;XXX 20010;JJJJJ33333;28;QQQ5;10922; 
XXX 2710;FFFFFFFFF;XXX 2710;LLLL2222;27;QQQ5;10922;

We have 2 files. Take column 4 (the field separator is :wink: from file1 search in file2 column 4 (the field separator is :wink: compare
if values are identical cut values from column 2, 3 and 5 (file2) and paste on the end of line in file1.

Can someone help with solution in awk?

$ awk 'NR==FNR {f2[$4]=$2 OFS $3 OFS $5} NR!=FNR && f2[$4] {print $0 f2[$4]}' FS=';' OFS=';' file2 file1

great working as should be, Thank you. could you please explain how exactly this is working?

'NR==FNR {
   f2[$4]=$2 OFS $3 OFS $5
}

For lines in the first file specified on the command line (total record number equals file record number): Set an array ('f2') element indexed by the value of field 4 to the concatenated value of fields 2, 3 and 5 (separated by the output field separator OFS).

NR!=FNR && f2[$4] {
   print $0 f2[$4]
}'

For lines not in the first file and where there exists an element in array f2 for index of field 4 (i.e. this matches a line from the first file): Print the current line followed by the value of the array element (which we set earlier to fields 2/3/5 from the other file).

 FS=';' OFS=';' file2 file1

Set the field separator and output field separator to semi-colon, pass in the files (reversing the files is so we drive the actual output from file1, which preserves the ordering and saves messing with extra arrays)

1 Like