2014-02-16 96 views
0

以下命令仅返回file2中的匹配项。grep:输出两个匹配

grep -f file1 file2 

如何从file2的第二个匹配之后的第一个文件(file1)打印匹配行?

回答

3
awk 'NR==FNR{res[$0]; next} 
{ 
    found = 0 
    for (re in res) { 
     if ($0 ~ re) { 
      print "found:", re 
      found = 1 
     } 
    } 
} 
found 
' file1 file2 
+0

非常感谢。你能告诉我如何在一行中打印匹配(分隔标签)吗? – EpiMan

+0

自己尝试打印格式'printf' – BMW

+0

@Ed Morton我也对部分匹配行感兴趣,但看起来你的代码只给出完整匹配,我是否正确?为此我使用了grep。 – EpiMan