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

3. 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

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

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

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

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

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/

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

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).

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

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

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"

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.

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

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