【问题标题】:Grep in multiple files prints matches line with file name多个文件中的 Grep 打印与文件名匹配的行
【发布时间】:2013-06-20 03:29:03
【问题描述】:

我正在使用 grep 从两个不同文件中的文件中查找匹配行。它可以很好地找到从File1File2File3 的匹配文件,但是从存在多个文件的那一刻起,它会在该行旁边打印找到它的文件名。

grep -w -f File1 File2 File3

输出:

File2: pattern

File2: pattern

File3: pattern

是否有避免打印File2:File3: 的选项?

【问题讨论】:

    标签: printing grep


    【解决方案1】:
    grep --no-filename -w -f File1 File2 File3
    

    【讨论】:

      【解决方案2】:

      如果您使用的是 UNIX 系统,请参阅手册页。每当您遇到问题时,您的第一步应该是man $programName。在这种情况下,man grep。看来您需要“-h”选项。以下是手册页的摘录:

         -h, --no-filename
                Suppress the prefixing of file names on output.  This is the default when there is only one file (or only standard input) to search.
      

      【讨论】: