grep

From RaySoft

grep is a command line text search utility originally written for Unix. The name is taken from the first letters in global / regular expression / print, a series of instructions in text editors such as ed. The grep command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.[1]

Documentation

Further information

Syntax

grep [PARAMETER ...] PATTERN [FILE ...]

Parameters

Matcher Selection

-E, --extended-regexp
Interpret PATTERN as an extended regular expression.
-P, --perl-regexp
Interpret PATTERN as a Perl regular expression.

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 -.
-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. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. 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
Suppress normal output; instead print a count of matching lines for each input file.
--color[=WHEN], --colour[=WHEN]
Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines) with escape sequences to display them in color on the terminal. [...] WHEN is never, always or auto. (GNU version only)
-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

-C NUMBER, -NUMBER, --context=NUMBER
Print NUMBER lines of output context. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.
-H, --with-filename
Print the file name for each match. This is the default when there is more than one file to search.
-n, --line-number
Prefix each line of output with the line number within its input file.

File and Directory Selection

--exclude=GLOB
Skip files whose base name matches GLOB (using wildcard matching). A file-name glob can use *, ?, and [...] as wildcards, and \ to quote a wildcard or backslash character literally.
--exclude-dir=DIR
Exclude directories matching the pattern DIR from recursive searches.
--include=GLOB
Search only files whose base name matches GLOB.
-R, -r, --recursive
Read all files under each directory, recursively; this is equivalent to the -d recurse option.

Examples

Ignore comments and empty lines
grep --invert-match --regexp='^\( *#\| *$\)' 'file.txt'
Grep for the keyword "Linux" in the current directory and below
grep --recursive 'Linux' '.'
The statement is true if there is at least one line fulfill the grep's regular expression
if grep --regexp='^ *$' 'file.txt' >'/dev/null' 2>&1; then
  echo 'Empty lines'
fi

References