Practical grep command tricks

grep-command

13 Grep commands

We can grep the text as required, searching with particular string, printing lines before and after the search string.

First I am going to create an file to explain how grep works. The demo files contains below lines

[root@arkit grep]# cat demofile
First line in the grep demo
Second line is this
ALL UPPERCASE CHARACTERS IN THIS LINE
below second line in this grep demo
last line of the grep demo

 1. Check grep command installed..? and its version

Redhat / Centos / Fedora use below ccommand

rpm -qa |grep grep

In Ubuntu Operating system

dpkg -l |grep grep

To check its version in all the OS

grep -V

2. Search single word in single file / multiple files case sensitive

To grep text as case sensitive no need to use any options, default grep will search the text as case sensitive. See the below example when searched with ‘first‘ word it does find matching in the file but when we search with ‘First‘ it find matching.

[root@arkit grep]# grep first demofile
[root@arkit grep]# grep First demofile
First line in the grep demo

grep command

3. Search case insensitive word using grep -i

To grep text as case insensitive we have to use -i option. So it matches all the words such as “first”, “FIRST” and “First” case insensitively as shown below.

[root@arkit grep]# grep -i first demofile
First line in the grep demo

grep command

4. Search text which is not matching to string

Below option is useful when your searching for the exclude matching word. As a example below it is excluded “First” line from the search.

You can also use multiple strings using -e option. see the other below example where we are excluding the ‘First‘ and ‘last‘ strings.

[root@arkit grep]# grep -v First demofile
Second line is this
ALL UPPERCASE CHARACTERS IN THIS LINE
below second line in this grep demo
last line of the grep demo

[root@arkit grep]# grep -v -e "First" -e "last" demofile
Second line is this
ALL UPPERCASE CHARACTERS IN THIS LINE
below second line in this grep demo

grep command

5. Print the matching string and its after number of lines

To print the string and its after number of lines, we have to use -A option. See the below example grepping the word ‘below‘ and mentioned number of lines after -A options it prints after immediate lines.

[root@arkit grep]# grep below -A 2 demofile
below second line in this grep demo
last line of the grep demo

grep command

6. Print the matching string and its before number of lines

To print the string and its before number of lines, we have to use -B option. See the below example grepping the word ‘below’ and mentioned number of lines after -B option it prints before number of lines.

[root@arkit grep]# grep below -B 2 demofile
Second line is this
ALL UPPERCASE CHARACTERS IN THIS LINE
below second line in this grep demo

grep command

7. Print the matching string and its around number of lines

To print matching string and its around lines we have to use -C option. See the below example grepping for the word ‘ALL’ and mentioned number of lines after -C option it print its above and its below line.

[root@arkit grep]# grep ALL -C 1 demofile
Second line is this           <-------its Above line 1
ALL UPPERCASE CHARACTERS IN THIS LINE
below second line in this grep demo  <-----its Below line 1

grep command

8. Search Recursively all the sub-directories

To search all the sub-directories we have to use -r flag.  See the below example to understand the recursive search.

[root@arkit ~]# grep -ril /root/
.cache/imsettings/log.bak
.cache/imsettings/log
.cache/tracker/meta.db
.cache/tracker/meta.db-wal

grep command

9. Grep the string with highlighted in color

Most of the times we search for matching strings but we have see in detailed that where is matched string is. If we see searched string in will show in highlighted color it will be most effective view we can see. So how we can set the grep highlight color lets see

Read More  Setting up Linux Lab - Creating and modifying VM Network

We can use –color option to see the string in color, as a temporary.

[root@arkit grep]# grep --color=auto CHARACTERS demofile
ALL UPPERCASE CHARACTERS IN THIS LINE

grep command

If you want to set this option as permanent we have to set the environment variable

[root@arkit grep]# export GREP_COLOR='1;30;42'

which basically highlights the matched pattern with foreground color black and background color yellow (shown below in the snap).

grep command

The set display attributes list:

0 Reset all attributes
1 Bright
2 Dim
4 Underscore
5 Blink
7 Reverse
8 Hidden

Foreground Colors
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White

Background Colors
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White

10. Get the count of given string from single file / multiple files

To count the matched string we have to use -c option. See the below example.

[root@arkit grep]# grep -c line demofile
4

grep command

11. Search for files which are matching to the given string

We can also search the files using grep command lets see how to search files. Below example we are searching for the demo* file.

[root@arkit grep]# grep -l this demo*
demofile

grep command

12. Beginning of line (^) using cap symbol

In grep command, caret Symbol ^ matches the expression at the start of a line. In the following example, it displays all the line which starts with the Oct 05. i.e All the messages logged on October 05.

[root@arkit grep]# grep "^Oct  6" /var/log/messages
Oct  6 09:56:46 localhost rsyslogd: [origin software="rsyslogd"

grep command

The ^ matches the expression in the beginning of a line, only if it is the first character in a regular expression. ^N matches line beginning with N.

Read More  11 best code review softwares list Nobody Will Tell You

13. End of the line ( $) using dollar symbol

Character $ matches the expression at the end of a line. The following command will help you to get all the lines which ends with the word ‘interrupt’

[root@arkit grep]# grep "interrupt$" /var/log/messages
Oct  6 09:56:23 localhost kernel: pcieport 0000:00:15.0: Signaling PME through PCIe PME interrupt
Oct  6 09:56:23 localhost kernel: pcieport 0000:00:15.1: Signaling PME through PCIe PME interrupt

grep command

From the above output you can come to know when all the messages has got interrupt. Just like ^ matches the beginning of the line only if it is the first character, $ matches the end of the line only if it is the last character in a regular expression.

This grep commands are most useful commands when we want to search for some strings in the files, searching for the particular files on the directories.

Do comment you feedback about this article.

Thanks for your wonderful Support.

Stay Connected with us. Learn More and Earn More

Good Luck..!!

Ankam Ravi Kumar

Working as Linux / Storage Administrator L3. Interested in sharing the knowledge.

You may also like...

2 Responses

  1. kv says:

    Hi

    I have records like below in one file

    asd wer(234) hh rty(567) uu
    tyu tgh(456) kk yui(456) oo

    I need to get only data in between braces like–>234 567

    and assign this to one variable for looping please tell me any command.I tried with cut command but it is
    fecthing –> 234) rty(567) (2nd & 4 th fields) from this we have to cut based on charecter please tell me is there any simple command to get data in between braces

  2. Gemma_D says:

    Hello,Ankam
    Great info and very useful for me .Thank you very much for sharing it.

Leave a Reply