Linux grep command with 14 different examples

Overview :

Linux like operating system provides a searching tool known as grep (global regular expression print). grep command is useful for searching the content of one more files based on the pattern. A pattern may be a single character, bunch of characters, single word or a sentence.

When we execute the grep command with specified pattern, if its is matched, then it will display the line of file containing the pattern without modifying the contents of existing file.

In this tutorial we will discuss 14 different examples of grep command

Example:1 Search the pattern (word) in a file

Search the “linuxtechi” word in the file /etc/passwd file

root@Linux-world:~# grep linuxtechi /etc/passwd
Example:2 Search the pattern in the multiple files.
root@Linux-world:~# grep linuxtechi /etc/passwd /etc/shadow /etc/gshadow
Example:3 List the name of those files which contain a specified pattern using -l option.
root@Linux-world:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab
Example:4 Search the pattern in the file along with associated line number(s) using the -n option
root@Linux-world:~# grep -n linuxtechi /etc/passwd

root@Linux-world:~# grep -n root /etc/passwd /etc/shadow


Example:5 Print the line excluding the pattern using -v option

List all the lines of the file /etc/passwd that does not contain specific word “linuxtechi”.

root@Linux-world:~# grep -v linuxtechi /etc/passwd


Example:6 Display all the lines that starts with specified pattern using ^ symbol

Bash shell treats carrot symbol (^) as a special character which marks the beginning of line or a word. Let’s display the lines which starts with “root” word in the file /etc/passwd.

root@Linux-world:~# grep ^root /etc/passwd
Example: 7 Display all the lines that ends with specified pattern using $ symbol.

List all the lines of /etc/passwd that ends with “bash” word.

root@Linux-world:~# grep bash$ /etc/passwd

Bash shell treats dollar ($) symbol as a special character which marks the end of line or word.

Example:8 Search the pattern recursively using -r option
root@Linux-world:~# grep -r linuxtechi /etc/

Above command will search linuxtechi in the “/etc” directory recursively.

Example:9 Search all the empty or blank lines of a file using grep
root@Linux-world:~# grep ^$ /etc/shadow

As there is no empty line in /etc/shadow file , so nothing is displayed.

Example:10 Search the pattern using ‘grep -i’ option.

-i option in the grep command ignores the letter case i.e it will ignore upper case or lower case letters while searching

Lets take an example , i want to search “LinuxTechi” word in the passwd file.

nextstep4it@localhost:~$ grep -i LinuxTechi /etc/passwd
Example:11 Search multiple patterns using -e option

For example i want to search ‘linuxtechi’ and ‘root’ word in a single grep command , then using -e option we can search multiple patterns .

root@Linux-world:~# grep -e "linuxtechi" -e "root" /etc/passwd
Example:12 Getting Search pattern from a file using “grep -f”

First create a search pattern file “grep_pattern” in your current working directory. In my case i have put the below contents.

root@Linux-world:~# cat grep_pattern

Now try to search using grep_pattern file.

root@Linux-world:~# grep -f grep_pattern /etc/passwd


Example:13 Count the number of matching patterns using -c option

Let take the above example , we can count the number of matching patterns using -c option in grep command.

root@Linux-world:~# grep -c -f grep_pattern /etc/passwd
Example:14 Display N number of lines before & after pattern matching

a) Display Four lines before patten matching using -B option

root@Linux-world:~# grep -B 4 "games" /etc/passwd


b) Display Four lines after pattern matching using -A option

root@Linux-world:~# grep -A 4 "games" /etc/passwd


c) Display Four lines around the pattern matching using -C option

root@Linux-world:~# grep -C 4 "games" /etc/passwd


2 Responses

  1. Malcolm Hutchison says:

    “-i option … i.e it will ignore upper case or lower case letters while searching”

    the “i.e ” description is not correct – it doesn’t ignore uppercase or lowercase letters (which would mean it ignores all letters) but it does not discriminate between upper and lower case lettesr, so uppercase A will match either uppercase or lowercase A.

  2. swathi says:

    Awesome article on grep command ..

Leave a Reply

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