首页 > 解决方案 > shell脚本比较两个文件并打印包含常用词的行?

问题描述

让我们考虑两个文件,文件 1 包含三个单词,文件 2 包含一些行。我想要的输出应该包含文件 2 中包含文件 1 和下 2 行中的单词的行

文件 1

EF-hand_motif
Ferritin
Manganese_catalase

文件 2

a_1.out:The conserved site of Ferritin is found as: ['EFKEAFSL', 'EAELQDMI', 'EMIREADI']
a_1.out:Length of conserved site: 32
a_1.out:Position: 0-31
g_1.out:The conserved site of EF-hand_motif is found as: ['DADGNGTIDFPE', 'DKDGNGYISAAE']
g_1.out:Length of conserved site: 28
g_1.out:Position: 0-27
d_1.out:The conserved site of Hemerythrin is found as: ['ELRH']
d_1.out:Length of conserved site: 4
d_1.out:Position: 100-103

想要的输出格式

a_1.out:The conserved site of Ferritin is found as: ['EFKEAFSL', 'EAELQDMI', 'EMIREADI']
a_1.out:Length of conserved site: 32
a_1.out:Position: 0-31
g_1.out:The conserved site of EF-hand_motif is found as: ['DADGNGTIDFPE', 'DKDGNGYISAAE']
g_1.out:Length of conserved site: 28
g_1.out:Position: 0-27

标签: shellgrep

解决方案


使用

$ grep -Ff file1 -A 2 file2
a_1.out:The conserved site of Ferritin is found as: ['EFKEAFSL', 'EAELQDMI', 'EMIREADI']
a_1.out:Length of conserved site: 32
a_1.out:Position: 0-31
g_1.out:The conserved site of EF-hand_motif is found as: ['DADGNGTIDFPE', 'DKDGNGYISAAE']
g_1.out:Length of conserved site: 28
g_1.out:Position: 0-27
  • -F表示匹配固定字符串而不是正则表达式,
  • -f file表示要匹配的读取模式file
  • -A nn表示在匹配行之后打印尾随上下文的行。

推荐阅读