Grep command tutorial in linux/unix with examples and use cases

linux grep command – print lines that match patterns

Linux grep searches for PATTERNS in each FILE. PATTERNS is one or patterns separated by newline characters, and grep prints each line that matches a pattern.

A FILE of “-” stands for standard input. If no FILE is given, recursive searches examine the working directory, and nonrecursive searches read standard input.

In addition, the variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively. These variants are deprecated, but are provided for backward compatibility.

Syntax

       grep [OPTION...] PATTERNS [FILE...]
       grep [OPTION...] -e PATTERNS ... [FILE...]
       grep [OPTION...] -f PATTERN_FILE ... [FILE...]

Options

Matcher Selection

  • -E, –extended-regexp
    Interpret PATTERNS as extended regular expressions.
  • -F, –fixed-strings
    Interpret PATTERNS as fixed strings, not regular expressions.
  • -G, –basic-regexp
    Interpret PATTERNS as basic regular expressions. This is the default.
  • -P, –perl-regexp
    Interpret PATTERNS as Perl-compatible regular expressions (PCREs). This option is experimental when combined with the -z (–null-data) option, and grep -P may warn of unimplemented features.

Matching Control

  • -e PATTERNS, –regexp=PATTERNS
    Use PATTERNS as the patterns. If this option is used multiple times or is combined with the -f (–file) option, search for all patterns given. This option can be used to protect a pattern beginning with “-”.
  • -f FILE, –file=FILE
    Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with the -e (–regexp) option, search for all patterns given. The empty file contains zero patterns, and therefore matches nothing.
  • -i, –ignore-case
    Ignore case distinctions, so that characters that differ only in case match each other.
  • -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. This option has no effect if -x is also specified.
  • -x, –line-regexp
    Select only those matches that exactly match the whole line. For a regular expression pattern, this is like parenthesizing the pattern and then surrounding it with ^ and $.
  • -y
    Obsolete synonym for -i.

General Output Control

  • -c, –count
    Suppress normal output; instead print a count of matching lines for each input file. With the -v, –invert-match option, count non-matching lines.
  • –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.
  • -L, –files-without-match
    Suppress normal output; 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
    Suppress normal output; 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. If the input is standard input from a regular file, and NUM matching lines are output, grep ensures that the standard input is positioned to just after the last matching line before exiting, regardless of the presence of trailing context lines. This enables a calling process to resume a search. When grep stops after NUM matching lines, it outputs any trailing context lines. When the -c or –count option is also used, grep does not output a count greater than NUM. When the -v or –invert-match option is also used, grep stops after outputting NUM non-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.
  • -q, –quiet, –silent
    Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
  • -s, –no-messages
    Suppress error messages about nonexistent or unreadable files.

Context Line Control

  • -A NUM, –after-context=NUM
    Print NUM lines of trailing context after matching lines.
  • -B NUM, –before-context=NUM
    Print NUM lines of leading context before matching lines.
  • -C NUM, -NUM, –context=NUM
    Print NUM lines of output context.

File and Directory Selection

  • -a, –text
    Process a binary file as if it were text; this is equivalent to the –binary-files=text option.
  • –binary-files=TYPE
  • -D ACTION, –devices=ACTION
    If an input file is a device, FIFO or socket, use ACTION to process it.
  • -d ACTION, –directories=ACTION
    If an input file is a directory, use ACTION to process it.
  • –exclude=GLOB
    Skip any command-line file with a name suffix that matches the pattern GLOB, using wildcard matching;
  • –exclude-from=FILE
    Skip files whose base name matches any of the file-name globs read from FILE.
  • –exclude-dir=GLOB
    Skip any command-line directory with a name suffix that matches the pattern GLOB.
  • –include=GLOB
    Search only files whose base name matches GLOB.
  • -r, –recursive
    Read all files under each directory, recursively, following symbolic links only if they are on the command line. Note that if no file operand is given, grep searches the working directory. This is equivalent to the -d recurse option.
  • -R, –dereference-recursive
    Read all files under each directory, recursively.

Examples

  • find a file named test
ylspiritdeMacBook-Pro:grep ylspirit$ ls | grep "test"
  • find a file whose name is not test
ylspiritdeMacBook-Pro:grep ylspirit$ ls | grep -v "test"

  • filter contains
ylspiritdeMacBook-Pro:grep ylspirit$ grep -a "13" *
  • recursive filter contains
ylspiritdeMacBook-Pro:grep ylspirit$ grep -aR "13" *

More use cases

grep multiple words/patterns/strings, and/or condition, use -e or regex

grep process by name/by id and kill example, and grep -v example

grep count lines/words, count sort

grep not include, grep reverse match

grep only show match

grep multiple patterns

grep show lines before and after

grep or , grep and , grep not condition example

grep regex example

linux ls only files/directories example


More about Linux awk command: linux awk syntax and awk examples
More about Linux commands: Linux Commands Tutorial

Add a Comment

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