Practical grep command tricks Search Millions Of Records

We can grep the text as required, searching with particular string, printing lines before and after the search string. Practical Grep command Tricks will help you to search millions of records. Search for required text content out of big text files is hectic task.

  • -c : Print only count which matches the pattern
  • -h : Display matched lines only
  • -i : Ignore Upper case or Lower case (Non-case sensitive)
  • -L: Without matching
  • -l : Display only file names
  • -n : Pattern matches lines and their line numbers
  • -v : Ignore the pattern and match except
  • -e exp : Expression with this option. Can use multiple times.
  • -f file : Takes patterns from file, one per line.
  • -E : Extended regular expressions or egrep
  • -w : Match pattern with whole word
  • -o : Print only the matched parts of a matching line, with each such part on a separate output line.

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

Red hat / Centos / Fedora use below command

rpm -qa |grep grep

In Ubuntu Operating system

dpkg -l |grep grep

To check its version in all the OS

grep -V

2. Search word in single/multiple files case sensitive

To grep text using grep command 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 command3. Case insensitive word using grep -i (ignore case sensitive)

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

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

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/

grep command

9. grep command 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

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 commandThe ^ 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.

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

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

[root@arkit grep]# grep "interrupt$" /var/log/messages

grep commandFrom 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 command 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.

Grep Video tutorial

Thanks for your wonderful Support and Encouragement

blank

Ankam Ravi Kumar

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

3 Responses

  1. blank 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. blank Gemma_D says:

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

  3. blank johnloi.study says:

    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
    ——————————-

    $ echo “asd wer(234) hh rty(567) uu” | grep -o ‘[[:digit:]]*’
    234
    567

    for i in $(echo “asd wer(234) hh rty(567) uu” | grep -o ‘[[:digit:]]*’); do echo “$i”; done;

    234
    567

Leave a Reply

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