I would like to combine entries from the second field from two files using awk, sed or similar.
File0:
string:data:moredata
File1:
string:random:moredata
If the first field, string in file0 has a matching entry in file1 then print
$random:$data
Selecting the fields seems trivial:
$ awk -F':' '{print $2}' filename
Need to match rows and print matching column $2
How about this one –
Execution:
In this solution, we are loading the whole record of file1 in to the array indexed at column 1. We do a quick check in the next file to see if the column 1 is present. If it is then print statement is executed.
Negative Test:
Just to add to the explanation, NR and FNR are awk’s in-built variables. NR gives line number and does not get reset to 0 when looped over two files. FNR on the contrary is also a line number that gets reset to 0 when second file starts. Thus this allows us to store the file 1 into the array because that action is only done when NR==FNR. As soon as this condition becomes false, it means the second file has started and next pattern action statement begins to execute.