Linux grep commands with examples

grep command is used to display lines matching with given pattern. grep has 3 different variants available.

  1. fgrep(Deprecated)     : Same as grep -F
  2. egrep(Deprecated)    : Same as grep -E
  3. rgrep                                 : Same as grep -r

GREP Command Example

grep -w JBT.COM grepFile.txt

grep                  : Command Name.

-w                      : An argument which determine how search will be performed.

“JBT.COM”   : String which will be searched

grepFile.txt   : File where above String will be searched

OPTIONS

There are several arguments available for grep command. I have divided them in 4 different areas(Officially there is no division, i did just to make you understand how they work).

  1. What to search
  2. How to search
  3. Where to search
  4. How to display result

What to search

-E, –extended-regexp     :  It supports special meaning of meta character({,|,+,(….).

-G, –basic-regexp             : It doesn’t support special meaning of meta character.

-F, –fixed-strings             : Same as fgrep(Fast grep). It has better performances than Other two above options because it drops regular expression all together.

How to search

-i, –ignore-case

-v, –invert-match

-w, –word-regexp (Exact word search)

-x, –line-regexp

Where to search

-a, –text                                            : Process binary file as text

-d ACTION                                     : It define how to process directory based on Action type. Action could be READ / SKIP / RECURSE.

–exclude-from=FILE                 : Skip files which has matching name.

–exclude-dir=DIR                      : Skip directory which has matching name

-r, –recursive                                 : Read all file under each directory recursively. Do not follow symbolic links.

-R, –dereference-recursive     : Read all file under each directory recursively. Follow symbolic links.

 

How to display result

-L, –files-without-match    : Name of each input file where no match found

-l, –files-with-matches        : Name of each input file where match found

-c, –count                                 : Print count of matching line for each input file.

-o, –only-matching               : Prints only matched part of line.

-q, –quiet, –silent                  : Do not write anything to standard output.

-H, –with-filename               : Prints the file name for each input.

-h, –no-filename                   : Do not display file name to standard output.

-n, –no of line in file             : Display line number within input file for each matched line.

Practical Example

I have created a 2 test file (grepFile.txt & grepFile_2.txt ) with all content on this page.

Scenario 1:

Search for “STANDARD OUTPUT” String(Case Insensitivity) in test file with line number.

grep -ni 'STANDARD OUTPUT' grepFile.txt grepFile_2.txt

Output

grepFile.txt:57:-q, --quiet, --silent: Do not write anything to standard output.
grepFile.txt:61:-h, --no-filename : Do not display file name to standard output.
grepFile_2.txt:57:-q, --quiet, --silent: Do not write anything to standard output.
grepFile_2.txt:61:-h, --no-filename : Do not display file name to standard output.

Scenario 2:

Search for all lines where “STANDARD OUTPUT” String(Case Insensitivity) is not available.

grep -vi 'STANDARD OUTPUT' grepFile.txt grepFile_2.txt

Output will be all lines except above 4 lines from two files.

Scenario 3:

Search for lines where String “exclude-dir” found. Also display 3 trailing lines where match found.

grep -A 3 -ni exclude-dir  grepFile.txt

Output

grepFile.txt:42:--exclude-dir=DIR: Skip directory which has matching name
grepFile.txt-43-
grepFile.txt-44--r, --recursive : Read all file under each directory recursively. Do not follow symbolic links.
grepFile.txt-45-
--

 

Scenario 4:

Search for lines where String “exclude-dir” found. Also display 3 previous lines where match found.

grep -B 3 -ni exclude-dir  grepFile.txt

Output

grepFile.txt-39-
grepFile.txt-40---exclude-from=FILE : Skip files which has matching name.
grepFile.txt-41-
grepFile.txt:42:--exclude-dir=DIR: Skip directory which has matching name
--

Scenario 5:

What if you want to display previous and trailing lines around match found? You can use -C for this purpose.

 grep -C 3 -ni exclude-dir  grepFile.txt

Output

grepFile.txt-39-
grepFile.txt-40---exclude-from=FILE : Skip files which has matching name.
grepFile.txt-41-
grepFile.txt:42:--exclude-dir=DIR: Skip directory which has matching name
grepFile.txt-43-
grepFile.txt-44--r, --recursive : Read all file under each directory recursively. <strong>Do not follow</strong> symbolic links.
grepFile.txt-45-
--

Scenario 6:

Display only number of match found.

grep -c -ni exclude-dir  grepFile.txt

Output will be 1.

Scenario 7:

Search for entire word(e.g. matching)

grep -niw matching  grepFile.txt

Output

40:--exclude-from=FILE : Skip files which has

matching

 name.
42:--exclude-dir=DIR: Skip directory which has

matching

 name
53:-c, --count : Print count of

matching

 line for each input file.
55:-o, --only-

matching

 : Prints only matched part of line

But if you search for “match”

grep -niw match  grepFile.txt

Output will not contain “matching” word. And output will be different as below.

30:-v, --invert-

match

49:-L, --files-without-match : Name of each input file where <em><strong>no

match

</strong></em> found
51:-l, --files-with-matches : Name of each input file where<em><strong>

match

</strong></em> found
s

Scenario 8:

Use regular expression to search in file.

grep -E -niw mat..  grepFile.txt

Here . will be used as any character. And output will be

30:-v, --invert-match
49:-L, --files-without-match : Name of each input file where <em>no match</em> found
51:-l, --files-with-matches : Name of each input file where<em> match</em> found

 

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.