Good day,
I have been trying to find a solution to a similar problem asked in comparing 2 files using awk but I can't seem to get my head around it. Looking for some kind assistance.
I have 2 files that I would like to compare. Mock-up contents of file1 and file2 are shown below:
file1:
50 0004312805201 06740 2310821
50 0004986504201 00845 2310837
50 0003913155201 47679 2310762
50 0004997395201 2035 2311180
50 0001147242201 15000 23108723
50 0005771878201 13545 I3840000
file2:
0003913155 A
0005771878 A
0004312805 A
0000000015 B
0000000012 B
1111111111 E
I need to perform a substring
on field2 from file1 to produce a 10 character length searchable key value
and find the matching value in field1 of file2.
If a match is found, print out the entire file1 row with field2 from file2 appended as a new field.
If no match, print out the entire file1 row with the string "NO" appended as a new field. Output would be best redirected to a file.
Example output is shown below.
Output:
50 0004312805201 06740 2310821 A
50 0004986504201 00845 2310837 NO
50 0003913155201 47679 2310762 A
50 0004997395201 2035 2311180 NO
50 0001147242201 15000 23108723 NO
50 0005771878201 13545 I3840000 A
How would you guys suggest I tackle this via awk
or GNU-awk
? Having issues in preparing the searchable key substring and using it in awk/GNU-awk
to build the array.
Any help would be greatly appreciated. I am spinning my wheels at this point.
Thanks.