逐行删除文件 A 中不包含文件 B 中匹配项的字段

G P*_*G P 2 awk grep

我有一系列配对文件,制表符分隔。

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

示例文件 A:

a b
d c
Run Code Online (Sandbox Code Playgroud)

示例文件 B:

f>543 h<456 b>536 d>834 v<75345 a>12343
t>4562 c>623 f>3246 h>1345 d<52312
Run Code Online (Sandbox Code Playgroud)

期望的输出:

b>536 a>12343
c>623 d<52312
Run Code Online (Sandbox Code Playgroud)

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

  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 技能。

Rav*_*h13 6

使用您显示的示例,请尝试以下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
Run Code Online (Sandbox Code Playgroud)

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

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.
Run Code Online (Sandbox Code Playgroud)