How to effectively use the GREP command

Grep is a very important & powerful Linux commands. It stands for ‘Global Regular Expression Print’ & is used for matching & printing a search pattern or a regular expression from a single or multiple text files. It will look for the pattern if the mentioned files & will than print the result on screen or to an output file.
In this tutorial, we are going to learn to use grep command with examples.

Syntax for the command is as follows,
$ grep option search_pattern file_path

Searching a pattern from a single file

To search & locate a pattern from a single file, use the following command,
$ grep danger example.txt
This will look for all the lines with word ‘danger’ in the file ‘example.txt’ & will produce the result on screen.

Search a pattern in multiple files

We can also search the pattern from multiple files, using the following command,
$ grep danger example.txt exe.txt
Another example would be,
$ grep danger *.*
This will look for search pattern ‘danger’ in all the files with any name or extension.

Redirecting output to a file

Just like we can do with any other command, we can also redirect the output of the grep command to a file, use
$ grep danger example.txt > output.txt

Case insensitive search

Grep searches are case sensitive by default. If we need to run case-insensitive search, we can use ‘i’ option with grep command,
$ grep -i danger example.txt
This will print any variation of search pattern ‘danger’, ‘DANGER’ or ‘Danger’ or ‘daNGer’ as the output on the screen.

Searching a pattern in sub-directories

We can also search a pattern in sub-directories & all the files they contain by using ‘r’ option,
$ grep -r danger /home/dan

Display line number for search pattern

To display the line number on the which search pattern has matched, we can use ‘n’ option with grep command,
$ grep -n danger example.txt

Highlight the matched pattern

To highlight the matched search pattern, we can use ‘color’ with grep command,
$ grep --color danger example.txt

Print lines starting with the pattern

To print all the lines as output on screen which starts with out search pattern, we can use ‘^’ (carrot symbol) option ,
$ grep ^danger example.txt

Print lines ending with the pattern

To print all the lines that end with the search pattern, use ‘$’ symbol,
$ grep danger$ example.txt

Print all lines excluding the matched ones

To print all the lines, excluding the lines which have the matched search pattern, we can use ‘v’ option,
$ grep -v danger example.txt

Combining grep with another command

We can also use grep command to search a pattern from another command’s output. For eample, we need to get the process information about httpd from all the running processes, we can combine grep command with ‘ps’ command,
$ ps -ef | grep httpd
This will bring process information for httpd. Similarly we can use this for other services & other commands as well. Another example,
$ netstat -anp | grep 3306
This will produce list of all connections with mysql database.

These were only some examples & we have only covered some of the most commonly used grep command’s options, but there are plenty more options, which you can see with ‘grep –help‘ command.


– masterkenneth

Leave a Reply

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