awk - 逐行删除文件 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
到目前为止,我已经尝试过:
将文件 B 转换为单行文件:
cat file B | sed 's/\t/\n/g' > file B.mod
从文件 B 中提取文件 A 中的一个字符串,打印匹配行和下一行,将输出从 2 行转换回单个制表符分隔行:
cat file B.mod | grep -A1 (string) | awk '{printf "%s%s",$0,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.
推荐阅读
- java - Java:如何将不包含空格的字符表示为单词?
- android - 使用意图从类向 MainActivity 发送数据时出错
- c# - 如何在一台 Windows 服务器上运行多个 asp.net core MQTT 代理?
- winapi - MFC:从控件本身的代码限制子控件的大小
- chocolatey - 创建 Chocolatey 包并多次执行安装
- java - 如何为井字游戏创建简单、中等和困难级别?
- python - Python“SyntaxError:无效语法”,我不知道为什么
- python - 我在 Python 中的范围有问题,变量声明为全局但仍然出错
- html - 设置背景图像以填充整个窗口
- javascript - 如何将选项的ID存储在javascript中的选择框数组中?