Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep without showing path/file:line

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 {} \; 
like image 718
Allan Thomas Avatar asked Oct 16 '13 14:10

Allan Thomas


People also ask

How do you grep silently?

The quiet option ( -q ), causes grep to run silently and not generate any output. Instead, it runs the command and returns an exit status based on success or failure. The return status is 0 for success and nonzero for failure.

How do I exclude a file type in grep?

To exclude a filetype, you use rg --type-not cpp , to search only for a filetype you use rg --type cpp .

How can I get only file names in grep?

One such option is -l or --files-with-matches . When -l or --files-with-matches is enabled, only the names of files containing selected lines are written to standard output. The path names are listed once per file searched.

How do I grep a list of files in a directory?

Search All Files in Directory To search all files in the current directory, use an asterisk instead of a filename at the end of a grep command. The output shows the name of the file with nix and returns the entire line.


1 Answers

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.

like image 135
fedorqui 'SO stop harming' Avatar answered Sep 18 '22 16:09

fedorqui 'SO stop harming'