grep without showing path/file:line

LinuxUnixGrepFind

Linux Problem Overview


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 directory, searching for the term FOO

find . -name '*.bar' -exec grep -Hn FOO {} \;

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

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

find . -name '*.bar' -exec grep -hn FOO {} \;

Solution 3 - Linux

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAllan ThomasView Question on Stackoverflow
Solution 1 - LinuxfedorquiView Answer on Stackoverflow
Solution 2 - LinuxjkshahView Answer on Stackoverflow
Solution 3 - LinuxTC1View Answer on Stackoverflow