How to use grep, egrep, frgep and rgrep command in Linux

How to use grep, egrep, frgep and rgrep command in Linux

Grep command is used for searching file pattern in Linux. There are many grep options you can use while using the grep tool in Linux or Unix. There are some grep examples given below, Using these examples in real life scenario you will be able to learn the grep usage. Although grep is not the only tool for searching pattern, there are other tools available. For a System administrator it is also very important to know how to replace the searched string with the desired string. Linux sed tutorial is one of our article which will guide you through.

General syntax of using grep

Below is the general syntax for grep usage.

grep <options> <pattern> <file name>

Check your logs with grep

Log files are used to monitor your system. You can also check the failed login attempts of any user. See the below example to become more clear. In the below example grep searches for the Failed string in the log file with the help of pipe.

# cat /var/log/auth.log | grep "Failed"


To search pattern in directory and its sub directory

When searching for any pattern or string you may have to look for it in the whole directories and its sub-directories. grep -r command is very useful for searching recursively. See the recursive grep usage below.

# grep -r "Sysadmin" employee
 employee/employee.txt:200 John Smith Sysadmin
 employee/employee1.txt:342 saina Sysadmin
 employee/employee3.txt:143 James kol Sysadmin
 employee/employee2.txt:530 Quen Sysadmin

The above command will search for the string recursively in the directory employee. But what if the string is not starting with the capital letter?. In that case the grep command cannot search the pattern.
Using -i option with grep will ignore the case sensitivity and search and find the string even if it contains any small or caps letter in it. Please see the below example.

# grep -r -i "sysadmin" employee

 

Search pattern in multiple files in the same directory

You can search for pattern in multiple files in the same directory.

# grep "string" file1 file2 file3

Search multiple strings in the same file

You may want to search for more than one string in a given file, then use the below command.

# grep "string1 string2" file

To search insensitive case string using grep

If you want to search for insensitive case grep search, then use grep -i

# grep -ri "sysadmin" employee

To search only the word not the whole line

The below command grep -o will search only the string that you are looking for, not the whole line.

# grep -oi "smith" employee.txt

To search and select only non-matching lines

Some time you may want to select only the non-matching line and exclude the matching lines. In that case use grep -v command

# cat employee.txt 
100    John Doe    DBA
200    John Smith    Sysadmin
300    Raj Patel    Developer
# grep -vi "john" employee.txt 
300 Raj Patel Developer

How to use grep -n to show the line numbers of searched pattern

To display the line numbers of the searched pattern use the below command.

# grep -n "saina" employee1.txt 
1:342 saina Sysadmin

Use grep -c command to count number of matched pattern in file

If you want to know how many patterns are matched, use the given command . This will count the number of matched patterns.

# cat employee.txt

100 John Doe DBA
200 John Smith Sysadmin
300 Raj Patel Developer
# grep -c "John" employee.txt 
2

Use grep -v -c to count number of lines which does not match the pattern

 Count lines which does not match the pattern. See the example given below and use it to test your self.

# grep -c -v "John" employee.txt 1

Using grep -l option to display files which matches the string

The below command will show you the list of files which matches the string.

# grep -l "Sysadmin" emplo*
employee1.txt
employee2.txt
employee3.txt
employee.txt

Using grep -A option to display number of lines after the matched pattern

Sometime you may also want to display lines after your matched string or pattern. Use grep -A and the mention the number of lines you want to display.

# ifconfig | grep -A 5 enp1s0

enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
 inet 192.168.0.8 netmask 255.255.255.0 broadcast 192.168.0.255
 inet6 fe80::224:8cff:feba:8e6b prefixlen 64 scopeid 0x20<link>
 ether 00:24:8c:ba:8e:6b txqueuelen 1000 (Ethernet)
 RX packets 11559 bytes 6898660 (6.5 MiB)
 RX errors 0 dropped 0 overruns 0 frame 0

Using grep -B option to display number of lines before the matched pattern

grep -B will display lines before the matched pattern.The below example is given for better understanding.

# ifconfig | grep -B 5 lo

 RX packets 12318 bytes 7626686 (7.2 MiB)
 RX errors 0 dropped 0 overruns 0 frame 0
 TX packets 11961 bytes 1683873 (1.6 MiB)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
 inet 127.0.0.1 netmask 255.0.0.0
 inet6 ::1 prefixlen 128 scopeid 0x10<host>
 loop txqueuelen 0 (Local Loopback)

Use grep --color to print the search pattern in color

 

If you want the output of your searched pattern in color then use the command given below. See the example given below.
Then string John will be printed in color.

# grep --color "John" employee.txt
grep
Use grep --color to print the search pattern in color

For more options in grep usage use the command given below.

# man grep

Using egrep

egrep is an acronym for Extended Global Regular Expression Print. You can also use grep -E instead of using egrep. Both will give the same result.

Using fgrep

fgrep evaluates strings as a fixed string. fgrep considers every character inside the string as a full fixed sring. One can also use grep -F instead of fgrep.

Using rgrep

rgrep is same as grep -r. It is useful in doing recursive grep search. It searches the whole directories and its sub-directories for the pattern.

 

Also see Linux command to find a file


That's all with How to use grep, egrep, frgep and rgrep command in Linux.

Download Our Free eBook now

Linux and UNIX Shell scripting ebook

Be the first to comment

Leave a Reply

Your email address will not be published.


*


shares