Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 5 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
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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