Unix - Examples for grep command
Article Number: 248 | Rating: Unrated | Last Updated: Fri, Jun 8, 2018 10:04 PM
Unix - Examples for grep command
Below are some examples on the usage of grep and egrep in Unix.
We are using file text.txt as our source file.
Content of text.txt
==================================================================
one
two
three
four
five
six
seven
eight
nine
==================================================================
To print NUM lines of trailing context after matching lines.
==================================================================
[root@server:~] $grep -A<NUM of lines> <pattern> <file>
[root@server:~] $grep -A2 six text.txt
six
seven
eight
==================================================================
To print NUM lines of leading context before matching lines.
==================================================================
[root@server:~] $grep -B<NUM of lines> <pattern> <file>
[root@server:~] $grep -B2 six text.txt
four
five
six
==================================================================
To print all except lines matching pattern.
==================================================================
[root@server:~] $grep -v <pattern> <file>
[root@server:~] $grep -v six text.txt
one
two
three
four
five
seven
eight
nine
==================================================================
Using egrep to print lines matching either pattern.
==================================================================
[root@server:~] $grep -v '<pattern1>|<pattern2>' <file>
[root@server:~] $egrep 'six|seven' text.txt
six
seven
==================================================================
We are using file text.txt as our source file.
Content of text.txt
==================================================================
one
two
three
four
five
six
seven
eight
nine
==================================================================
To print NUM lines of trailing context after matching lines.
==================================================================
[root@server:~] $grep -A<NUM of lines> <pattern> <file>
[root@server:~] $grep -A2 six text.txt
six
seven
eight
==================================================================
To print NUM lines of leading context before matching lines.
==================================================================
[root@server:~] $grep -B<NUM of lines> <pattern> <file>
[root@server:~] $grep -B2 six text.txt
four
five
six
==================================================================
To print all except lines matching pattern.
==================================================================
[root@server:~] $grep -v <pattern> <file>
[root@server:~] $grep -v six text.txt
one
two
three
four
five
seven
eight
nine
==================================================================
Using egrep to print lines matching either pattern.
==================================================================
[root@server:~] $grep -v '<pattern1>|<pattern2>' <file>
[root@server:~] $egrep 'six|seven' text.txt
six
seven
==================================================================