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

 [mks_separator style=”dashed” height=”4″]

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

 [mks_separator style=”dashed” height=”4″]

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.

 [mks_separator style=”dashed” height=”4″]

How to search

-i, –ignore-case

-v, –invert-match

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

-x, –line-regexp

 [mks_separator style=”dashed” height=”4″]

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.

 [mks_separator style=”dashed” height=”4″]

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.

[mks_separator style=”dashed” height=”2″]

Practical Example

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

[mks_separator style=”dashed” height=”3″]

Scenario 1:

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

Output

[mks_separator style=”dashed” height=”3″]

Scenario 2:

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

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

[mks_separator style=”dashed” height=”3″]

Scenario 3:

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

Output

[mks_separator style=”dashed” height=”3″]

Scenario 4:

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

Output

[mks_separator style=”dashed” height=”3″]

Scenario 5:

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

Output

[mks_separator style=”dashed” height=”3″]

Scenario 6:

Display only number of match found.

Output will be 1.

[mks_separator style=”dashed” height=”3″]

Scenario 7:

Search for entire word(e.g. matching)

Output

But if you search for “match”

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

[mks_separator style=”dashed” height=”3″]

Scenario 8:

Use regular expression to search in file.

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

 

 

Leave A Comment

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