简体   繁体   English

使用 awk 打印匹配键的列范围

[英]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.我想在密钥文件中查找主文件的第二列,对于任何匹配的密钥,只打印前两列,但打印 rest 的整个记录。我有一个工作脚本,但打印整行对于匹配的键。 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

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 .当我们在key数组中找到$2 2 时,使用三元运算打印$1 OFS $2否则我们打印$0
  • used column -t for tabular outut用于表格输出的column -t

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.最后一个1表示一个空块,其默认行为是打印整行。
And combined with the next in the preceding block, acts as a kind of if else switch.并与前面块中的next结合,充当一种if else开关。

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.假设您不关心 output 行的顺序,那么以下内容也可能对您有所帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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