Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

NAME

grep - print lines matching a pattern

SYNOPSIS

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

DESCRIPTION

grep searches the named input FILEs, or standard input if no files are named, for lines containing a match to the given PATTERN. By default, grep prints the matching lines.

OPTIONS

-c
Suppress normal output; instead print a count of matching lines for each input file. With the -v option, count non-matching lines.

-e PATTERN
Use PATTERN as the pattern. This can be used to specify multiple search patterns.

-f FILE
Obtain patterns from FILE, one per line. This can be used to protect a pattern beginning with a hyphen (-).

-H
Print the file name for each match. This is the default when there is more than one file to search.

-h
Suppress the prefixing of file names on output. This is the default when there is only one file to search.

-i
Ignore case distinctions in the input files.

-L
Suppress normal output; instead print the name of each input file from which no output would normally have been printed.

-l
Suppress normal output; instead print the name of each input file from which output would normally have been printed.

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

-r
Read all files under each directory recursively.

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

EXAMPLES

To search for user tariq under /etc/passwd using \grep command:

Code Block
grep tariq /etc/passwd

To force grep to ignore word case i.e mach tariq, Tariq, TARIQ and all other combinations use the -i option:

Code Block
grep -i "TARIQ" /etc/passwd

To search recursively use the -r option

Code Block
grep -r "192.168.1.5" /etc/

To count lines when words are matched use the -c option with grep
Sample File:
1>\cat pattern-test.txt
Mary had a little lamb
Mary fried a lot of spam
Jack ate a Spam sandwich
Jill had a lamb spamwich

Code Block
grep -c Mary pattern-test.txt 

Grep invert match, use the -v option

Code Block
grep -v Mary pattern-test.txt

To print the filename for each match together with the matched line, use the -H option:

Code Block
grep -H Mary *.txt

To suppress the filenames on output, use the -h option

Code Block
grep -h Mary *.txt

To print the name of files in a directory, which does not match the pattern, use the -L option

Code Block
grep -L Mary *.txt

To list the name of matching files with the specified pattern use the -l option

Code Block
grep -l Mary *.txt

To combine grep with other availabe commands in the fluidshell use pipes

Code Block
cat pattern-test.txt | grep -h Mary *.txt

NOTES

If both -H and -h are specified, headers are not displayed.
If both -L and -l are specified, -L takes precedence.
If both -c and -r are specified, the output of count on directories is suppressed.
 If either -L or -l is specified, the scanning will stop on the first match.
The following commands might generate a 'cat: file: Pipe closed' warning if a match is found:

Code Block
cat file | grep -l pattern
cat file | grep -L pattern