逐行删除文件 A 中不包含文件 B 中的匹配项的字段
Remove fields in file A that don't contain matches in file B, line by line
我有一系列配对文件,制表符分隔。
我想逐行比较每一对,并在文件 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
到目前为止我已经尝试过:
在单行文件中转换文件 B:
cat file B | sed 's/\t/\n/g' > file B.mod
从文件B中Grep文件A中的一个字符串,打印匹配行和下一行,将输出从2行转换回单个制表符分隔行:
cat file B.mod | grep -A1 (string) | awk '{printf "%s%s",[=14=],NR%2?"\t":"\n" ; }'
...但这失败了,因为我意识到匹配项在 A 和 B 中的顺序可能不同,如上例所示。
非常感谢您的帮助,因为这远远超出了我的 bash 技能范围。
使用您显示的示例,请尝试以下 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.
我有一系列配对文件,制表符分隔。
我想逐行比较每一对,并在文件 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
到目前为止我已经尝试过:
在单行文件中转换文件 B:
cat file B | sed 's/\t/\n/g' > file B.mod
从文件B中Grep文件A中的一个字符串,打印匹配行和下一行,将输出从2行转换回单个制表符分隔行:
cat file B.mod | grep -A1 (string) | awk '{printf "%s%s",[=14=],NR%2?"\t":"\n" ; }'
...但这失败了,因为我意识到匹配项在 A 和 B 中的顺序可能不同,如上例所示。
非常感谢您的帮助,因为这远远超出了我的 bash 技能范围。
使用您显示的示例,请尝试以下 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.