首页 > 解决方案 > 逐行删除文件 A 中不包含文件 B 中匹配项的字段

问题描述

我有一系列配对文件,标签分隔。

我想逐行比较每一对,并在文件 B 中只保留与配对文件 A 匹配的字段。

示例文件 A:

a b
d c

示例文件 B:

f>543 h<456 b>536 d>834 v<75345 a>12343
t>4562 c>623 f>3246 h>1345 d<52312

期望的输出:

b>536 a>12343
c>623 d<52312

到目前为止,我已经尝试过:

  1. 将文件 B 转换为单行文件: cat file B | sed 's/\t/\n/g' > file B.mod

  2. 从文件 B 中提取文件 A 中的一个字符串,打印匹配行和下一行,将输出从 2 行转换回单个制表符分隔行:

cat file B.mod | grep -A1 (string) | awk '{printf "%s%s",$0,NR%2?"\t":"\n" ; }'

...但这失败了,因为我意识到匹配项在 A 和 B 中的顺序可能不同,如上面的示例所示。

我会很感激一些帮助,因为这远远超出了我的 bash 技能。

标签: awkgrep

解决方案


使用您显示的示例,请尝试以下awk代码。

awk '
FNR==NR{
  for(i=1;i<=NF;i++){
    arr[FNR,$i]
  }
  next
}
{
  val=""
  for(i=1;i<=NF;i++){
    if((FNR,substr($i,1,1)) in arr){
      val=(val?val OFS:"")$i
    }
  }
  print val
}
'  filea  fileb

说明:为上述添加详细说明。

awk '                         ##Starting awk Program from here.
FNR==NR{                      ##Checking condition FNR==NR which will be true when filea is being read.
  for(i=1;i<=NF;i++){         ##Traversing through all fields here.
    arr[FNR,$i]               ##Creating array with index of FNR,current field value here.
  }
  next                        ##next will skip all further statements from here.
}
{
  val=""                      ##Nullify val here.
  for(i=1;i<=NF;i++){         ##Traversing through all fields here.
    if((FNR,substr($i,1,1)) in arr){ ##checking condition if 1st letter of each field with FNR is present in arr then do following.
      val=(val?val OFS:"")$i  ##Creating val which has current $i value in it and keep adding values per line here.
    }
  }
  print val                   ##Printing val here.
}
'  filea  fileb               ##Mentioning Input_file names here.

推荐阅读