简体   繁体   中英

Using awk to print range of columns for matching keys

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

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

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.

Here is one more approach for doing same operation. Assuming you don't care of order of output lines, then following may help you too.

awk '
FNR==NR{
  arr1[$2]=$1 OFS $2
  arr2[$2]=$0
  next
}
($1 in arr1){
  print arr1[$1]
  arr3[$1]
}
END{
  for(key in arr2){
    if(!(key in arr3)){
      print arr2[key]
    }
  }
}
' mainfile keyfile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM