Linux grep Filters and options descriptions

The Unix and Linux grep command stands for global regular expression print. The system grep command filters the content of a file which makes our search easy.

grep with pipe

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

Sample syntax of grep with ( | ) command :

# command | grep <searchword>

Like example of grep ( | ) command :

# cat test | grep 9

Look at the above picture, grep command filters all the data containing ‘9’

grep without pipe

It can be used without pipe also.

Sample syntax of grep with command :

# grep <searchwork> <filename>

Like example of grep command :

# grep 9 test

Look at the above picture, grep command do the same work as earlier example but without pipe.

grep options

grep -v M

The Unix or Linux system grep -v command displays lines not matching to the specified word.

Sample syntax of grep with -v command :

# grep -v <searchwork> <filename>

Like example of grep-v 9 command :

# grep -v 9 test

Look at the above picture, command “grep -v 9 test” displays lines which don’t contain our search word ‘9’.

grep -i

System of Linux grep -i command filters output in a case-insensitive way.

Sample syntax of grep with -i command :

# grep -i <searchwork><filename>

Like example of grep -i command :

grep -i red test

Look at the above picture, command “grep -i red test” displays all lines containing ‘red’ whether in upper case or lower case.

grep -A/ grep -B/ grep -C

grep -A

This command is used to display the line after the result.

grep -B

This command is used to display the line before the result.

grep -C

This command is used to display the line after and line before the result.

You can use (A1, A2, A3…..)(B1, B2, B3….)(C1, C2, C3….) to display any number of lines.

Sample syntax of grep with grep -A/ grep -B/ grep -C command :

#grep -A<lineNumber> <searchWord> <fileName>  
#grep -B<lineNumber> <searchWord> <fileName>  
#grep -C<lineNumber> <searchWord> <fileName>  

Like example of grep -A/ grep -B/ grep -C command :

#grep -A1 yellow test  
#grep -B1 yellow test  
#grep -C1 yellow test  

Look at the above Picture, command “grep -A1 yellow test” displays searched line with next succeeding line, The command grep -B1 yellow test displays searched line with one preceding line and system command grep -C1 yellow test” displays searched line with one preceding and succeeding line.

You can now practice your own ability with interest. If you practice more and more then you will can create your expertness. If you understand this article you can must practice your terminal. I hope you can learn something in your linux environment. Please learn linux system with your own freedom.

This Post Has One Comment

Leave a Reply