Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep output to show only matching file

Tags:

grep

What is the option for grep that will allow me only to print the matching file and not the line within a file that matches the criteria?

like image 504
paultop6 Avatar asked Oct 11 '10 16:10

paultop6


People also ask

How do I print only a grep matched string?

Grep: Print only the words of the line that matched the regular expression, one per line. We used the following parameters on our command: -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.

How do I grep a filename in a directory?

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.

How do you grep for a word and display only the word?

The easiest of the two commands is to use grep's -w option. This will find only lines that contain your target word as a complete word. Run the command "grep -w hub" against your target file and you will only see lines that contain the word "hub" as a complete word.

How do I filter a grep file?

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".


2 Answers

grep -l  

(That's a lowercase L)

like image 131
a'r Avatar answered Sep 28 '22 06:09

a'r


You can use the Unix-style -l switch – typically terse and cryptic – or the equivalent --files-with-matches – longer and more readable.

The output of grep --help is not easy to read, but it's there:

-l, --files-with-matches  print only names of FILEs containing matches 
like image 28
Iain Samuel McLean Elder Avatar answered Sep 28 '22 06:09

Iain Samuel McLean Elder