grep without showing path/file:line

前端 未结 3 534
难免孤独
难免孤独 2020-11-30 19:59

How do you grep and only return the matching line? i.e. The path/filename is omitted from the results.

In this case I want to look in all .bar files in the current d

相关标签:
3条回答
  • 2020-11-30 20:28

    Just replace -H with -h. Check man grep for more details on options

    find . -name '*.bar' -exec grep -hn FOO {} \;
    
    0 讨论(0)
  • 2020-11-30 20:36

    No need to find. If you are just looking for a pattern within a specific directory, this should suffice:

    grep -hn FOO /your/path/*.bar
    

    Where -h is the parameter to hide the filename, as from man grep:

    -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.

    Note that you were using

    -H, --with-filename

    Print the file name for each match. This is the default when there is more than one file to search.

    0 讨论(0)
  • 2020-11-30 20:52

    From the man page:

    -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.
    
    0 讨论(0)
提交回复
热议问题