Coder Perfect

without displaying path/file:line grep

Problem

How do you use grep to just return the lines that match? i.e. the path/filename is not included in the output.

In this scenario, I want to search for the term FOO in all.bar files in the current directory.

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

Asked by Allan Thomas

Solution #1

There’s no need to look. This should serve if you’re only looking for a pattern within a certain directory:

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

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

Keep in mind that you were utilizing

Answered by fedorqui ‘SO stop harming’

Solution #2

Simply substitute -h for -H. For additional information on options, see man grep.

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

Answered by jkshah

Solution #3

The following is taken 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.

Answered by TC1

Post is based on https://stackoverflow.com/questions/19406761/grep-without-showing-path-fileline