grep (global regular expression print) command in Linux

The grep command filters the content of a file which makes our search easy.

The grep command is generally used with pipe (|).

Syntax :

command | grep <searchWord>  

Output :
grep-pipe-linux

The grep command can also be used without a pipe.

Syntax :

grep <searchWord> <file name> 

Output :
grep-command-linux

Options :

grep -c: Displays only the count of matching lines

Syntax :

grep -c <searchWord> <fileName>  

Output :
grep-c-command-linux

grep -n: Shows the matching line and its number

Syntax :

grep -n <searchWord> <fileName>  

Output :
grep-n-command-linux

grep -l: Shows just the name of the file with the string

Syntax :

grep -l <searchWord> <fileName>  

Output :
grep-l-command-linux

grep -v: The 'grep -v' command displays the content which does not match the specified word.

Syntax :

grep -v <searchWord> <fileName>  

Output :
grep-v-command-linux

grep -i: The 'grep -i' command filters the data in a case-insensitive way.

Syntax :

grep -i <searchWord> <fileName>  

Output :
grep-i-command-linux

grep -A/ grep -B/ grep -C

Syntax :

//Command is used to display the line after the result.
grep -A<lineNumber> <searchWord> <fileName>  
//Command is used to display the line before the result.
grep -B<lineNumber> <searchWord> <fileName>
//Command is used to display the line after and line before the result.  
grep -C<lineNumber> <searchWord> <fileName>  

Output :
grep-a-command-linux

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions