Grep Command Examples.

The grep is a Linux utility command which will help you in searching the contents within the file. It is the most useful when you are debugging through a large message file. It can be helpful in the scripts too.

Below are the few examples and the tricks for grep command.

Grep Command to highlight Color

Below command will search for “DB Error” in the /var/log/messages and highlight it with red color so you can quickly identify.

[root@server ~]# grep --color "DB Error" /var/log/messages

Print 3 lines before and after the pattern match in grep

If you want to print the lines before/after the pattern matches then you can use -A (after) or -B (before) option. Below command will print 3 lines before and after match.

[root@server ~]# grep "DB Error" /var/log/messages -A3 -B3

Leave a Reply

Your email address will not be published. Required fields are marked *