如何使用匹配的词打印 grep 结果?
How can I print a grep result with the matched word?
我有两个文件,“文件 A”:
Adygei
Albanian
Armenia_C
Armenia_Caucasus
Armenia_EBA
Armenia_LBA
Armenia_MBA
Armenian.DG
Austria_EN_HG_LBK
Austria_EN_LBK
和“文件B”:
HG01880.SG Aygei_o1.SG
HG01988.SG Adygei_o2.SG
HG02419.SG Albanian_o2.SG
HG01879.SG Albanian.SG
HG01882.SG Armenia_C.SG
HG01883.SG Armenia_C.SG
HG01885.SG Armenia_EBA.SG
HG01886.SG Armenia_EBA.SG
HG01889.SG Armenia_LBA.SG
HG01890.SG Armenia_MBA.SG
最后我想要的是创建一个新的列(与列的位置无关),其中包含 grep 词和匹配的词。像这样:
HG01880.SG Aygei_o1.SG Adygei
HG01988.SG Adygei_o2.SG Adygei
HG02419.SG Albanian_o2.SG Albanian
HG01879.SG Albanian.SG Albanian
HG01882.SG Armenia_C.SG Armenia_C
HG01883.SG Armenia_C.SG Armenia_C
HG01885.SG Armenia_EBA.SG Armenia_EBA
HG01886.SG Armenia_EBA.SG Armenia_EBA
HG01889.SG Armenia_LBA.SG Armenia_LBA
HG01890.SG Armenia_MBA.SG Armenia_MBA
我用来匹配bash中两个文件的是grep -wFf fileA fileB > newfileA_B.txt
。这可以在 python 或 bash
中
您可以尝试类似的方法:
for line in $(cat fileA.txt)
do
echo "$line $(grep $line fileB.txt)"
done
这是 Python 中的一个示例(可能效率低下)算法(使用字符串而不是文件)https://colab.research.google.com/drive/1bUnFXJg0m6FvXRkPybUqWux_reaJRt1c?usp=sharing
再次执行 grep
搜索,但这次添加标志 -o
,它只列出匹配的词。然后使用 paste
将其添加为列(使用 -d
标志定义分隔符)。
paste -d ' ' <(grep -wFf fileA fileB) <(grep -woFf fileA fileB)
我有两个文件,“文件 A”:
Adygei
Albanian
Armenia_C
Armenia_Caucasus
Armenia_EBA
Armenia_LBA
Armenia_MBA
Armenian.DG
Austria_EN_HG_LBK
Austria_EN_LBK
和“文件B”:
HG01880.SG Aygei_o1.SG
HG01988.SG Adygei_o2.SG
HG02419.SG Albanian_o2.SG
HG01879.SG Albanian.SG
HG01882.SG Armenia_C.SG
HG01883.SG Armenia_C.SG
HG01885.SG Armenia_EBA.SG
HG01886.SG Armenia_EBA.SG
HG01889.SG Armenia_LBA.SG
HG01890.SG Armenia_MBA.SG
最后我想要的是创建一个新的列(与列的位置无关),其中包含 grep 词和匹配的词。像这样:
HG01880.SG Aygei_o1.SG Adygei
HG01988.SG Adygei_o2.SG Adygei
HG02419.SG Albanian_o2.SG Albanian
HG01879.SG Albanian.SG Albanian
HG01882.SG Armenia_C.SG Armenia_C
HG01883.SG Armenia_C.SG Armenia_C
HG01885.SG Armenia_EBA.SG Armenia_EBA
HG01886.SG Armenia_EBA.SG Armenia_EBA
HG01889.SG Armenia_LBA.SG Armenia_LBA
HG01890.SG Armenia_MBA.SG Armenia_MBA
我用来匹配bash中两个文件的是grep -wFf fileA fileB > newfileA_B.txt
。这可以在 python 或 bash
您可以尝试类似的方法:
for line in $(cat fileA.txt)
do
echo "$line $(grep $line fileB.txt)"
done
这是 Python 中的一个示例(可能效率低下)算法(使用字符串而不是文件)https://colab.research.google.com/drive/1bUnFXJg0m6FvXRkPybUqWux_reaJRt1c?usp=sharing
再次执行 grep
搜索,但这次添加标志 -o
,它只列出匹配的词。然后使用 paste
将其添加为列(使用 -d
标志定义分隔符)。
paste -d ' ' <(grep -wFf fileA fileB) <(grep -woFf fileA fileB)