Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To understand the practical use of Grep's option -H in different situations

Tags:

grep

option

This question is based on this answer.

Why do you get the same output from the both commands?

Command A

$sudo grep muel *                                                             /tmp
masi:muel

Command B

$sudo grep -H muel *                                                          /tmp
masi:muel

Rob's comment suggests me that Command A should not give me masi:, but only muel.

In short, what is the practical purpose of -H?

like image 368
Léo Léopold Hertz 준영 Avatar asked Jul 13 '09 21:07

Léo Léopold Hertz 준영


People also ask

What is the use of cat touch command explain with example?

cat command: It is used to create the file with content. touch command: It is used to create a file without any content. The file created using touch command is empty. This command can be used when the user doesn't have data to store at the time of file creation.

What is the purpose of option in grep command?

The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and print out). Options Description -c : This prints only a count of the lines that match a pattern -h : Display the matched lines, but do not display the filenames.

What is grep command explain with example?

To search for a string in a file, run the command below Syntax $ grep "string" file name. OR $ filename grep "string" Example: $ grep "Linux" welcome.txt. Output As you can see, grep has not only searched and matched the string “Linux” but has also printed the lines in which the string appears.


1 Answers

Grep will list the filenames by default if more than one filename is given. The -H option makes it do that even if only one filename is given. In both your examples, more than one filename is given.

Here's a better example:

$ grep Richie notes.txt
Richie wears glasses.

$ grep -H Richie notes.txt
notes.txt:Richie wears glasses.

It's more useful when you're giving it a wildcard for an unknown number of files, and you always want the filenames printed even if the wildcard only matches one file.

like image 132
RichieHindle Avatar answered Oct 24 '22 17:10

RichieHindle