grep Command in Linux

Back

The grep command uses a search term to look through a file. It returns the full line that contains the search term .

For examples :

1. Search for the given string in a single file
      Syntax:
    $ grep "literal_string" filename


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

2. Checking for the given string in multiple files.
    Syntax:
    $ grep "string" FILE_PATTERN

3. Case insensitive search using 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.


4. Match regular expression in files
    Syntax:
    $ grep "REGEX" filename
This is a very powerful feature, if you can use 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.

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

If you want to search for a word, and to avoid it to match the substrings use -w option. Just doing out a normal search will show out 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 out “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.


6. 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. In the following example, it will highlight all the 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.

7. Searching in all files recursively using grep -r

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

    $ grep -r "ramesh" *

8. Invert match using grep -v

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

When you want to display the lines which does 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”.

9. Counting the number of matches using grep -c

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

    Syntax:
    $ grep -c "pattern" filename

    $ grep -c "go" demo_text
    6

When you want do find out how many lines matches the pattern

    $ grep -c this demo_file
    3

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

    $ grep -v -c this demo_file
    4