grep

print lines matching a pattern

Syntax

grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

Description

grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.

In addition, two variant programs egrep and fgrep are available. egrep is the same as grep -E. fgrep is the same as grep -F. Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified.

Options

Matcher Selection
       -E, --extended-regexp
              Interpret PATTERN as an extended regular expression.

       -F, --fixed-strings
              Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.

       -G, --basic-regexp
              Interpret PATTERN as a basic regular expression. This is the default.

Matching Control
       -e PATTERN, --regexp=PATTERN
              Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-).

       -f FILE, --file=FILE
              Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing.

       -i, --ignore-case
              Ignore case distinctions in both the PATTERN and the input files.

       -v, --invert-match
              Invert the sense of matching, to select non-matching lines.

       -w, --word-regexp
              Select only those lines containing matches that form whole words. Word-constituent characters are letters, digits, and the underscore.

       -x, --line-regexp
              Select only those matches that exactly match the whole line.

General Output Control
       -c, --count
              Instead print a count of matching lines for each input file.

       -L, --files-without-match
              Instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match.

       -l, --files-with-matches
              Instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.

       -m NUM, --max-count=NUM
              Stop reading a file after NUM matching lines.

       -o, --only-matching
              Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

Output Line Prefix Control
       -H, --with-filename
              Print the file name for each match.

       -h, --no-filename
              Suppress the prefixing of file names on output.

       -n, --line-number
              Prefix each line of output with the 1-based line number within its input file.

File and Directory Selection
       -a, --text
              Process a binary file as if it were text

       --exclude-from=FILE
              Skip files whose base name matches any of the file-name globs read from FILE

       --exclude-dir=DIR
              Exclude directories matching the pattern DIR from recursive searches.

       -R, -r, --recursive
              Read all files under each directory, recursively

Example 1

Print all the lines from a file containing a given keyword using grep command.

[eleiss@eleiss ~]$ cat You_sang_to_me.txt 
Oh...
I just wanted you to comfort me
When I called you late last night you see
I was fallin' into love
Yes, I was crashin' into love
Oh of all the words you sang to me
About life, the truth and being free, yeah
You sang to me, oh how you sang to me

[eleiss@eleiss ~]$ grep "love" You_sang_to_me.txt
I was fallin' into love
Yes, I was crashin' into love
[eleiss@eleiss ~]$

The file that cat command displays the content of contains a stanza of a popular song "You sang to me" by Marc Anthony.

Later the grep command prints all the lines of the file which contains the keyword "love".

By default a case sensitive search is performed. In order to ignore case you can use -i option with grep command.

Example 2

Print all the lines from a file matching more than one patterns using grep command.

[eleiss@eleiss ~]$ grep -e "love" -e "you" You_sang_to_me.txt 
I just wanted you to comfort me
When I called you late last night you see
I was fallin' into love
Yes, I was crashin' into love
Oh of all the words you sang to me
You sang to me, oh how you sang to me
[eleiss@eleiss ~]$

In this example the grep uses the same file as the previous example uses.

In this example the grep prints all the lines from the file matching the patterns "love" and/or "you"

Example 3

Print all the lines starting with alphabetical characters in a given range.

[eleiss@eleiss ~]$ cat register 
Hardik	Desai		1
Arpit	Dudhwala	10
Pooja	Chauhan		8
Arpit	Patel		44
Jimmy	Paanwala	36
Naitik	Modi		25
Kartik	Bhatarkar	5
Pooja	Shah		57
[eleiss@eleiss ~]$ cat register | grep ^[a-m]
Hardik	Desai		1
Arpit	Dudhwala	10
Arpit	Patel		44
Jimmy	Paanwala	36
Kartik	Bhatarkar	5
[eleiss@eleiss ~]$

In this example, the special character "^" imposes grep command to print all the lines matching a pattern that starts with alphabetical characters starting from "a" to "h"

Example 4

Demonstrate inverted match in grep command.

[eleiss@eleiss ~]$ cat register
Hardik	Desai		1
Arpit	Dudhwala	10
Pooja	Chauhan		8
Arpit	Patel		44
Jimmy	Paanwala	36
Naitik	Modi		25
Kartik	Bhatarkar	5
Pooja	Shah		57
[eleiss@eleiss ~]$ cat register | grep -iv "arpit"
Hardik	Desai		1
Pooja	Chauhan		8
Jimmy	Paanwala	36
Naitik	Modi		25
Kartik	Bhatarkar	5
Pooja	Shah		57
[eleiss@eleiss ~]$

In this example, grep matches the pattern "arpit" ignoring the case (-i option) and prints all the lines on he standard output that do not contain the pattern.

Example 5

Count the number lines that match the given pattern using grep command.

[eleiss@eleiss ~]$ cat register 
Hardik	Desai		1
Arpit	Dudhwala	10
Pooja	Chauhan		8
Arpit	Patel		44
Jimmy	Paanwala	36
Naitik	Modi		25
Kartik	Bhatarkar	5
Pooja	Shah		57
[eleiss@eleiss ~]$ cat register | grep -c "Pooja"
2
[eleiss@eleiss ~]$

As you can see that the file "register" contains two lines that contain the pattern "Pooja". Instead of printing the lines on the standard output, the grep command counts the number of matched lines and display that number on the standard output.

Example 6

Instead of printing the entire line of a file print only the matching pattern using grep command.

[eleiss@eleiss ~]$ cat register 
Hardik	Desai		1
Arpit	Dudhwala	10
Pooja	Chauhan		8
Arpit	Patel		44
Jimmy	Paanwala	36
Naitik	Modi		25
Kartik	Bhatarkar	5
Pooja	Shah		57
[eleiss@eleiss ~]$ cat register | grep -o "P.*a"
Pooja	Chauha
Pa
Paanwala
Pooja	Sha
[eleiss@eleiss ~]$ 

In this example the grep command prints only the strings that match the given pattern which is a regular exapression.


You may write to resources@eleiss.com for any Linux related queries.

advertisement