Grep -The Most useful Command in Linux

sunil-9
9 min readNov 19, 2022

--

In this blog, we will discuss in detail about Grep command in Unix/Linux which is the short form of ‘global search for the regular expression’ A regular expression is a string of characters that is used to specify a pattern matching rule. Special characters are used to define the matching rules and positions..

The grep command is a filter that is used to search for lines matching a specified pattern and print the matching lines to standard output.

grep command Linux Syntax:

grep [options] [pattern] [file]

1) Anchor Characters: ‘^’ and ‘$’ at the beginning and end of the pattern are used to anchor the pattern to the start of the line, and to the end of the line respectively.

Example: “^Name” matches all lines that start with the string “Name”. The strings “\<” and “\>” are used to anchor the pattern to the start and end of a word respectively.

2) Wildcard Character: ‘.’ Is used to match any character.

Example:“^.$” will match all lines with any single character.

3) Escaped Characters: Any of the special characters can be matched as a regular character by escaping them with a ‘\’.

Example: “\$\*” will match the lines that contain the string “$*”

4) Character Range: A set of characters enclosed in a ‘[‘ and ‘]’ pair specify a range of characters to be matched.

Example: “[aeiou]” will match all lines that contain a vowel. A hyphen can be used while specifying a range to shorten a set of consecutive characters. E.g. “[0–9]” will match all lines that contain a digit. A carat can be used at the beginning of the range to specify a negative range. E.g. “[^xyz]” will match all lines that do not contain x, y or z.

5) Repetition Modifier: A ‘*’ after a character or group of characters is used to allow matching zero or more instances of the preceding pattern.

The grep command supports a number of options for additional controls on the matching:

  • -i: performs a case-insensitive search.
  • -n: displays the lines containing the pattern along with the line numbers.
  • -v: displays the lines not containing the specified pattern.
  • -c: displays the count of the matching patterns.

grep command in Linux with an example

First, create the following demo_file that will be used in the examples below to demonstrate the grep command.

cat demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
and
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.


Two lines above this line is empty.
And this is the last line.

Search for the given string in a single file

The basic usage of the grep command is to search for a specific string in the specified file as shown below.

Syntax:
grep "literal_string" filename
$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

Checking for the given string in multiple files.

Syntax:
grep "string" FILE_PATTERN

This is also a basic usage of the grep command. For this example, let us copy the demo_file to demo_file1. The grep output will also include the file name in front of the line that matched the specific pattern as shown below. When the Linux shell sees the meta character, it does the expansion and gives all the files as input to grep.

$ cp demo_file demo_file1
$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.

Case insensitive search using command grep -i

Syntax:
grep -i "string" FILE

This is also a basic usage of the grep. This searches for the given string/pattern case insensitively. So it matches all the words such as “the”, “THE” and “The” case insensitively as shown below.

$ grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.

Match regular expression in files

Syntax:
grep "REGEX" filename

This is a very powerful feature if you can use regular expression effectively. In the following example, it searches for all the pattern that starts with “lines” and ends with “empty” with anything in-between. i.e To search “lines[anything in-between]empty” in the demo_file.

$ grep "lines.*empty" demo_file
Two lines above this line is empty.

From documentation of grep: A regular expression may be followed by one of several repetition operators:

  • ? The preceding item is optional and matched at most once.
  • * The preceding item will be matched zero or more times.
  • + The preceding item will be matched one or more times.
  • {n} The preceding item is matched exactly n times.
  • {n,} The preceding item is matched n or more times.
  • {,m} The preceding item is matched at most m times.
  • {n,m} The preceding item is matched at least n times, but not more than m times.

Checking for full words, not for sub-strings using grep -w

If you want to search for a word, and to avoid it matching the substrings use the -w option. Just doing a normal search will show all the lines.

The following example is the regular grep where it is searching for “is”. When you search for “is”, without any option it will show “is”, “his”, “this” and everything which has the substring “is”.

$ grep -i "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.

The following example is the WORD grep where it is searching only for the word “is”. Please note that this output does not contain the line “This Line Has All Its First Character Of The Word With Upper Case”, even though “is” is there in the “This”, as the following is looking only for the word “is” and not for “this”.

$ grep -iw "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

Displaying lines before/after/around the match using grep -A, -B, and -C

When doing a grep on a huge file, it may be useful to see some lines after the match. You might feel handy if grep can show you not only the matching lines but also the lines after/before/around the match.

Please create the following demo_text file for this example.

$ cat demo_text
4. Vim Word Navigation
You may want to do several navigation in relation to the words, such as: * e - go to the end of the current word.
* E - go to the end of the current WORD.
* b - go to the previous (before) word.
* B - go to the previous (before) WORD.
* w - go to the next word.
* W - go to the next WORD.
WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word * 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

Display N lines after the match

-A is the option that prints the specified N lines after the match as shown below.

Syntax:
grep -A <N> "string" FILENAME

The following example prints the matched line, along with the 3 lines after it.

$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

Display N lines before the match

-B is the option that prints the specified N lines before the match.

Syntax:
grep -B <N> "string" FILENAME

When you had the option to show the N lines after the match, you have the -B option for the opposite.

$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD

Display N lines around the match

-C is the option that prints the specified N lines before the match. On some occasions, you might want the match to appear with the lines from both sides. This option shows N lines on both sides (before & after) of the match.

$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word* 192.168.1.1 - single WORD

Highlighting the search using GREP_OPTIONS

As grep prints out lines from the file by the pattern/string you had given, if you wanted it to highlight which part matches the line, then you need to follow the following way.

When you do the following export you will get the highlighting of the matched searches. The following example, it will highlight all this when you set the GREP_OPTIONS environment variable as shown below.

$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
$ grep this demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

Searching in all files recursively using grep -r

When you want to search all the files under the current directory and its subdirectory. -r option is the one that you need to use. The following example will look for the string “ramesh” in all the files in the current directory and all its subdirectory.

$ grep -r "ramesh" *

Invert match using grep -v

You had different options to show the lines matched, to show the lines before the match, to show the lines after the match, and to highlight the match. So definitely You’d also want the option -v to do an inverted match.

When you want to display the lines which do not matches the given string/pattern, use the option -v as shown below. This example will display all the lines that did not match the word “go”.

$ grep -v "go" demo_text
4. Vim Word Navigation
You may want to do several navigation in relation to the words, such as:WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

display the lines which does not matches all the given pattern.

Syntax:
grep -v -e "pattern" -e "pattern"
$ cat test-file.txt
a
b
c
d
$ grep -v -e "a" -e "b" -e "c" test-file.txt
d

Counting the number of matches using grep -c

When you want to count how many lines match the given pattern/string, then use option -c.

Syntax:
grep -c "pattern" filename
$ grep -c "go" demo_text
6

When you want to find out how many lines match the pattern

$ grep -c this demo_file
3

When you want to find out how many lines that do not match the pattern

$ grep -v -c this demo_file
4

Display only the file names which match the given pattern using grep -l

If you want the grep to show only the file names which matched the given pattern, use the -l (lower-case L) option.

When you give multiple files to the grep as input, it displays the names of file which contains the text that matches the pattern, will be very handy when you try to find some notes in your whole directory structure.

$ grep -l this demo_*
demo_file
demo_file1

Show only the matched string

By default grep will show the line which matches the given pattern/string, but if you want the grep to show out only the matched string of the pattern then use the -o option.

It might not be that much useful when you give the string straightforward. But it becomes very useful when you give a regex pattern and try to see what it matches as

$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line

Conclusion

grep is the most powerful and useful command line tool that can be used in daily life as a Linux user. It will save a lot of time and effort in searching a particular subject in a folder or a file.

--

--

sunil-9

Sunil Sapkota is a Full Stack Developer and Devops Engineer from Nepal. He is a passionate developer who loves to build things for the web.