The grep command is one of the most essential commands in Unix/Linux ecosystem, It is used to extract match and find the occurrence of string literals and patterns among the text files.
In this section we will focus on the important scenarios and provide the realtime explanation about the Various Grep command options offered in linux command line.
We now have a text file with Following content.
Below is the content of the file called intro.txt
.
[vamshi@linuxcent grep]$ cat intro.txt the first line is in lower case THE SECOND LINE IS IN UPPER CASE There following are Most popular Linux server Distributions: 1) Redhat Enterprise Linux 2) Ubuntu 3) Centos 4) Debian 5) SUSE Linux Enterprise Server This is a Demonstration of Linux grep command
Let’s start with search of a string pattern in the given file
The grep
command sample search syntax is
$ grep “string pattern” <filename>
$ cat < filename | * > | grep "string pattern"
Lets see the Demonstration
[vamshi@linuxcent grep]$ grep "Linux" intro.txt There following are Most popular Linux server Distributions: 1) Redhat Enterprise Linux 5) SUSE Linux Enterprise Server This is a Demonstration of Linux grep command
Searching for a pattern on multiple files using grep command
Lets see the Demonstration
[vamshi@linuxcent grep]$ grep "Linux" intro.txt intro-demo.txt intro.txt:There following are Most popular Linux server Distributions: intro.txt:1) Redhat Enterprise Linux intro.txt:5) SUSE Linux Enterprise Server intro.txt:This is a Demonstration of Linux grep command intro-demo.txt:There following are Most popular Linux server Distributions: intro-demo.txt:1) Redhat Enterprise Linux intro-demo.txt:5) SUSE Linux Enterprise Server intro-demo.txt:This is a Demonstration of Linux grep command
How to print the sequence number of matched pattern in file using Grep command in Linux?
Print the sequence number of lines that contain the matched string pattern can be achieved using -n
Option:
[vamshi@linuxcent grep]$ grep -n "Linux" intro.txt 4:There following are Most popular Linux server Distributions: 5:1) Redhat Enterprise Linux 9:5) SUSE Linux Enterprise Server 11:This is a Demonstration of Linux grep command
The -b
Option prints the subsequent starting offset index of the matching word from the lines within the given filename.
[vamshi@linuxcent grep]$ grep -b "Linux" intro.txt 66:There following are Most popular Linux server Distributions: 127:1) Redhat Enterprise Linux 184:5) SUSE Linux Enterprise Server 217:This is a Demonstration of Linux grep command
So far we have seen only the matching string pattern, but if you want to find the occurance of particular word then use grep -w
Option
Lets us see the Demonstration below:
[vamshi@linuxcent grep]$ grep -w "Cent" intro.txt
And As expected here we have not seen any occurrence of the word Cent from the file although the word Centos was present, But it didnt match because we used -w
option to match the word Cent.
But when we grep for the Word Linux then we can see the exact occurrence of the word and the matching lines are printed.
[vamshi@linuxcent grep]$ grep -w "Linux" intro.txt There following are Most popular Linux server Distributions: 1) Redhat Enterprise Linux 5) SUSE Linux Enterprise Server This is a Demonstration of Linux grep command
This can be used in conjunction with -o
prints only completely matched word string pattern.
[vamshi@linuxcent grep]$ grep -w -o "Linux" intro.txt Linux Linux Linux Linux
How to use the Invert Option in Linux grep command
The -v
option prints all the lines not matching the given string pattern.
[vamshi@linuxcent grep]$ grep -v "Linux" intro.txt the first line is in lower case THE SECOND LINE IS IN UPPER CASE 2) Ubuntu 3) Centos 4) Debian
How to print the names of all the files matching the string pattern using Linux Grep command.
Using the -l Option of grep command will print all the file names containing the matching string pattern/string literal
[vamshi@node02 grep]$ grep -l Linux * intro-demo.txt intro.txt
How to get the number of Count Matches using Grep command ?
In Linux Grep command we can use the Option -c
to get the total occurance Count of the matching pattern/string
Lets see the Demonstration below
[vamshi@linuxcent grep]$ grep -c "Linux" intro.txt 4
How to Exclude specific Directories while searching with grep command ?
The --exclude-dir="Some Directory name"
options skips the contents of the mention directory Lets see the Demonstration:
$ grep -w -E "data" * -R --exclude-dir="backup" --exclude-dir="adb-fastboot" --exclude-dir="IntelliJ-IDEA"
How to use Linux Grep commmand to ignore the case OR perform Case Insensitive search ?
Linux Grep Command offers the -i
to skip the case and perform the search.
[vamshi@linuxcent grep]$ grep -i Cent intro.txt 3)Centos
Lets search for the Work Upper from the file with -i
option
[vamshi@linuxcent grep]$ grep Upper -i intro-demo.txt THE SECOND LINE IS IN UPPER CASE
How to Grep for the lines before and After the pattern occurrence in a file
We can use the below options in Linux Grep command to extract the N number of lines matching the containing pattern before/After from the given file.. Lets look at the Options in detail
Generic Syntax:
grep -<A|B|C> “string pattern” filename*
Lets see the Demonstration as follows:
-A
: Prints N number of lines After the pattern match and the line containing the match
[vamshi@linuxcent grep]$ grep -A2 docker /etc/passwd dockerroot:x:996:992:Docker User:/var/lib/docker:/sbin/nologin mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin
-B
: Prints N number of lines Before the pattern match and the line containing the match
[vamshi@linuxcent grep]$ grep -B2 docker /etc/passwd apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin builduser1:x:1002:1002::/home/builduser1:/bin/bash dockerroot:x:996:992:Docker User:/var/lib/docker:/sbin/nologin
-C
: Prints the line Containing the pattern and the N number of lines Before and After
[vamshi@linuxcent grep]$ grep -C2 docker /etc/passwd apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin builduser1:x:1002:1002::/home/builduser1:/bin/bash dockerroot:x:996:992:Docker User:/var/lib/docker:/sbin/nologin mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin
Linux Grep command using the Regular Expression (regex) search pattern
How to use the Special Meta-characters in Grep Command
The Special Meta-Character ^
(caret) symbol to match expression at the start of a line.
Practical examples of grep command using meta-characters are as follows:
[vamshi@linuxcent grep]$ grep ^root /etc/services rootd 1094/tcp # ROOTD rootd 1094/udp # ROOTD
$
(dollar) symbol to match expression at the end of a line
[vamshi@linuxcent grep]$ grep "bash$" /etc/passwd root:x:0:0:root:/root:/bin/bash vagrant:x:1000:1000:vagrant:/home/vagrant:/bin/bash vamshi:x:1001:1001::/home/vamshi:/bin/bash jenkins:x:997:994:Jenkins Automation Server:/var/lib/jenkins:/bin/bash builduser1:x:1002:1002::/home/builduser1:/bin/bash linuxcent:x:1003:1004::/home/linuxcent:/bin/bash
Another Bonus regex for evading Empty lines from a text file using the Meta-characters ^
Carat and $
Dollar Symbols.
[vamshi@node02 grep]$ grep -Ev "^$" intro.txt the first line is in lower case THE SECOND LINE IS IN UPPER CASE There following are Most popular Linux server Distributions: 1) Redhat Enterprise Linux 2) Ubuntu 3) Centos 4) Debian 5) SUSE Linux Enterprise Server This is a Demonstration of Linux grep command
As the result of the above command observed Empty lines in our text file are now excluded with the inverted matching of Empty line pattern expression.
Digit Matching operation using Linux Grep Command
The Grep command Option -P
offers the perl-regex pattern matching to perform some of the tricky pattern matching conditions, Lets see the Demonstration.
[vamshi@linuxcent grep]$ grep -P "[\W][\d][\d]/" /etc/services
Now we Demonstrate Numerical pattern matching in the following ways to match the exact occurrence of IPv4 address
[vamshi@linuxcent grep]$ ip a |grep -E "[0-9].[0-9].[0-9].[0-9]" inet 127.0.0.1/8 scope host lo inet 10.100.0.20/24 brd 10.100.0.255 scope global noprefixroute eth1
The similar result can be extracted using -P
, Demonstrated as below:
[vamshi@linuxcent grep]$ ip a | grep -P "[\d].[\d].[\d].[\d]" inet 127.0.0.1/8 scope host lo inet 10.100.0.20/24 brd 10.100.0.255 scope global noprefixroute eth1
Highlighting the Search patterns with Color codes
If you want to highlight the search pattern from the output we can make use of Color code Option is grep
[vamshi@linuxcent ~]$ ps -ef |grep java --color=yes
grep command and the Meta-characters with practical examples
In the Extended Regular Expression the metacharacters ?, \+, \{, \|, \(, and \) enhances and takes the search string to manifest into a rich pattern
The square brackets [/code][ ][/code] includes a list of characters and it matches a single character in the list. Within the bracket, we can specify a range, like [a-z] [0-9] [abcd], But it only matches a single character in the give list range.
The brackets can contain the carat ^
or the $
symbols and can be globbed with the wildcard characters for finding the repetitive pattern.
Here we shall see the demonstration of a few metacharacters conjunction into a single grep command
Using the Infix Operator |
with the meta-characters
$ dmesg -H | egrep '(s|h)d[a-z]'
A quick practical look at the dmesg to find about the errors and warnings using grep command.
$ dmesg -H | grep -Ei “error|warn”
How to find the exact work match for the word error or err with a blank white space at the start of the word from dmesg?
$ dmesg -H | grep -Ei “(er)[r]{1,2}(or)”
$ dmesg -H | grep -Ei “(\We)[r]+(or)|(\Werr)”
Explanation: The grep search metacharacter regular expression matches the occurring of the string literals “error” with the minimum 1 time occurrence (error) of literal r and maximum 2 times.
The output only prints lines containing the word “error“ in a case insensitive grep search.
Another great practical example here we apply the same logic to enhance the metacharacter regex pattern to search for the occurrence of error with but without a trailing colon “:” symbol
dmesg -H | grep -Ei “(error)[:]{0}”