LinuxCommandLibrary

grep

Search text for matching patterns

TLDR

Search for a pattern within a file

$ grep "[search_pattern]" [path/to/file]
copy

Search for an exact string (disables regexes)
$ grep [[-F|--fixed-strings]] "[exact_string]" [path/to/file]
copy

Search for a pattern in all files recursively in a directory, showing line numbers of matches, ignoring binary files
$ grep [[-rnI|--recursive --line-number --binary-files=without-match]] "[search_pattern]" [path/to/directory]
copy

Use extended regexes (supports ?, +, {}, (), and |), in case-insensitive mode
$ grep [[-Ei|--extended-regexp --ignore-case]] "[search_pattern]" [path/to/file]
copy

Print 3 lines of [C]ontext around, [B]efore or [A]fter each match
$ grep [--context|--before-context|--after-context] 3 "[search_pattern]" [path/to/file]
copy

Print file name and line number for each match with color output
$ grep [[-Hn|--with-filename --line-number]] --color=always "[search_pattern]" [path/to/file]
copy

Search for lines matching a pattern, printing only the matched text
$ grep [[-o|--only-matching]] "[search_pattern]" [path/to/file]
copy

Search stdin for lines that do not match a pattern
$ cat [path/to/file] | grep [[-v|--invert-match]] "[search_pattern]"
copy

SYNOPSIS

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

PARAMETERS

-i
    Ignores case distinctions in both the PATTERN and the input files.

-v
    Inverts the match; selects non-matching lines.

-r, -R
    Recursively searches directories. -R follows symbolic links.

-l
    Lists only the names of files that contain matches, not the matching lines.

-c
    Suppresses normal output; instead, prints a count of matching lines for each input file.

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

-w
    Selects only those lines containing matches that form whole words.

-x
    Selects only those matches that match the entire line.

-E
    Interprets PATTERN as an extended regular expression (ERE). Similar to egrep.

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

-A NUM
    Prints NUM lines of trailing context after a matching line.

-B NUM
    Prints NUM lines of leading context before a matching line.

-C NUM
    Prints NUM lines of context around a matching line (combines -A and -B).

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

--color
    Highlights the matched strings in the output.

-q
    Quiet mode; suppresses all normal output. Useful for scripting when only the exit status matters.

-h
    Suppresses the prefixing of file names on output when multiple files are searched.

-e PATTERN
    Specifies a PATTERN. Useful for patterns starting with a hyphen (-) or to specify multiple patterns.

-f FILE
    Obtains patterns from FILE, one per line.

DESCRIPTION

The grep command is a powerful command-line utility used to search for specific text patterns within one or more files. Its name, an acronym for "global regular expression print," originates from the ed editor's g/re/p (global / regular expression / print) command. grep excels at filtering lines that match a given regular expression, making it indispensable for system administrators, developers, and users alike.

It can be used to search log files for errors, find specific code snippets in source directories, filter the output of other commands via pipes, and identify configuration settings. grep supports various types of regular expressions, from basic (BRE) to extended (ERE) and sometimes Perl-compatible (PCRE), providing immense flexibility in defining complex search patterns.

CAVEATS

When dealing with a very large number of files or extremely large individual files, grep can be resource-intensive. Performance might degrade if complex regular expressions are used on extensive datasets. Care must be taken when using special characters in PATTERN, as they often require escaping (e.g., using \ or single quotes) to prevent shell interpretation. Different versions of grep (GNU grep, BSD grep, etc.) and locale settings can subtly affect regular expression behavior and Unicode handling.

<I>REGULAR EXPRESSIONS</I>

The core power of grep lies in its ability to interpret regular expressions (regex). These are sequences of characters that define a search pattern. grep supports Basic Regular Expressions (BRE) by default, Extended Regular Expressions (ERE) with -E or egrep, and in some versions, Perl Compatible Regular Expressions (PCRE) with -P. Understanding regex is crucial for leveraging grep's full capabilities, allowing for flexible and precise pattern matching, including character sets, quantifiers, anchors, and grouping.

<I>EXIT STATUS</I>

grep communicates its success or failure through its exit status, which is vital for scripting. An exit status of 0 indicates that one or more lines were selected. An exit status of 1 means no lines were selected. An exit status of 2 indicates an error occurred (e.g., inaccessible file, invalid option). This allows scripts to make decisions based on whether a pattern was found.

HISTORY

The grep utility dates back to the early days of Unix, created by Ken Thompson in 1973. Its name is an acronym for "global / regular expression / print" and comes from a command in the ed text editor, g/re/p, which performs a global search for a regular expression and prints all matching lines. grep quickly became a fundamental Unix tool, embodying the Unix philosophy of doing one thing well. Over time, variants like egrep (for extended regular expressions) and fgrep (for fixed strings) emerged, later often integrated back into the main grep command via options like -E and -F. Its enduring design and widespread use underscore its importance in the command-line ecosystem.

SEE ALSO

awk(1), sed(1), find(1), less(1), more(1), cat(1), locate(1), pcregrep(1)

Copied to clipboard