Issue
this seems to have an easy solution, but I am stuck. I would like to look up the second column of a main file in a key file, and for any matched key, print only the first 2 columns, but the entire record for the rest. I have a working script but that prints the entire line for the matched keys. Can you please help?
awk 'FNR == NR {key[$1]; next} $2 in key {print $1,$2}' keyfile mainfile > outfile
mainfile:
PSHELL 10 136514 0.7
PSHELL 15 136514 0.7
PSHELL 20 136513 2.0
PSHELL 30 13571 1.7
keyfile:
10
30
outfile:
PSHELL 10
PSHELL 15 136514 0.7
PSHELL 20 136513 2.0
PSHELL 30
Solution
Try this:
awk 'FNR == NR {key[$1]; next} $2 in key {print $1,$2;next} 1' keyfile mainfile
The last 1
denotes an empty block whose default behavior is to print the whole line.
And combined with the next
in the preceding block, acts as a kind of if else
switch.
Answered By - Tiw