shell脚本来比较两个文件并打印包含常用词的行?

时间:2019-04-15 06:10:41

标签: shell grep

让我们考虑两个文件,文件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

1 个答案:

答案 0 :(得分:2)

使用

$ 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 n表示在匹配行之后打印尾随上下文的n行。