I can't seem to find any documentation dealing with this: is there any way to stop grep from printing the path to the file of a matched line when searching over multiple files?

link|improve this question
feedback

2 Answers

up vote 4 down vote accepted

From man grep:

-h, --no-filename
Suppress the prefixing of filenames on output when multiple files are searched.

Usage:

grep -h 'search term' multiple files

Example:

$ grep network apache2 svnserve sudo 
apache2:# Required-Start:    $local_fs $remote_fs $network $syslog $named
apache2:# Required-Stop:     $local_fs $remote_fs $network $syslog $named
svnserve:# Required-Start:    $network $remote_fs $syslog
svnserve:# Required-Stop:     $network $remote_fs $syslog
$ grep -h network apache2 svnserve sudo
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
link|improve this answer
feedback

there are 2 switches to grep that can help you here

  • --exclude, here you can directly provide file-names/paths to excluded using wildcard characters ( & escaping the actually required wildcard character)

    example: grep a ./* --exclude=*.log

  • --exclude-from, here you can provide a filename containing those strings to be matched and excluded

I'm using GNU grep 2.5.4, in case it's not available using your grep version.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.