Home > Software engineering >  Using awk to print range of columns for matching keys
Using awk to print range of columns for matching keys

Time:10-06

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

CodePudding user response:

You may use this awk:

awk 'FNR == NR {key[$1]; next} {print ($2 in key ? $1 OFS $2 : $0)}' keyfile mainfile | column -t > outfile

cat outfile

PSHELL  10
PSHELL  15  136514  0.7
PSHELL  20  136513  2.0
PSHELL  30

Here:

  • Used ternary operation to print $1 OFS $2 when we find $2 in key array otherwise we print $0.
  • used column -t for tabular outut

CodePudding user response:

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.

  • Related