Linux: text search command grep

Related Reading

Linuxicon-default.png?t=N7T8https://blog.csdn.net/weixin_45791458/category_12234591.html

grep is used in Unix-like systems to search and print lines that match a certain pattern in files. The basic syntax of the grep command is as follows:

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

grep searches named input files, i.e., the last [FILE…] (or standard input if there is no file or if a single hyphen -(-) is given as a filename) for lines that match the given PATTERN. By default, grep prints matching lines.

In addition, there are two variant commands egrep and fgrep. egrep is the same as grep -E. fgrep is the same as grep -F. Calling egrep or fgrep directly is not recommended, but it allows older programs that previously used them to run unmodified.

The options of grep are divided into several categories, which are introduced below.

1. Overall program information

–help

Usage: Print a usage message briefly summarizing the command line options and the error reporting address, then exit.

-V, –version

Usage: Print the version number of grep to the standard output stream. This version number should be included in all bug reports (see below).

2. Matcher selection

-E, –extended-regexp

Usage: Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)

-F, –fixed-strings, –fixed-regexp

Usage: Interpret PATTERN as a fixed string rather than a list of regular expressions (if there are multiple items), separated by newlines, any one of which must be matched. (-F is specified by POSIX, –fixed-regexp is an obsolete alias, please do not use it.)

-G, –basic-regexp

Usage: Interpret PATTERN as a basic regular expression (BRE, see below). This option is the default.

-P, –perl-regexp

Usage: Interpret PATTERN as a Perl regular expression. This is highly experimental, so grep -P may issue warnings about unimplemented features.

3. Matching control

-e PATTERN, –regexp=PATTERN

Usage: Use PATTERN as pattern. This can be used to specify multiple search patterns, or to protect patterns starting with a hyphen (-). (-e is specified by POSIX.)

-f FILE, –file=FILE

Usage: Get patterns from FILE, one per line. An empty file contains zero patterns and therefore matches nothing. (-f is specified by POSIX.)

-i, –ignore-case

Usage: Ignore case differences in PATTERN and input files. (-i is specified by POSIX.)

-v, –invert-match

Usage: Reverse the meaning of the match and select rows that do not match PATTERN. (-v is specified by POSIX.)

-w, –word-regexp

Usage: Used to select only lines that contain matches that form whole words. Specifically, the -w option requires that the matching substring be either at the beginning or end of the line, or be preceded and followed by a non-word-forming character (other than letters, numbers, and underscores).

-x, –line-regexp

Usage: Select only rows whose PATTERN exactly matches the entire row. (-x is specified by POSIX.)

-y

Usage: obsolete synonym for -i

4. Overall output control

-c, –count

Usage: Instead of printing matching lines, print the matching line count for each input file. Count unmatched lines when using the -v, –invert-match option. (-c is specified by POSIX.)

–color[=WHEN], –colour[=WHEN]

Usage: Surround the matching (non-empty) string, match line, context line, filename, line number, byte offset and delimiter (field for context line) with escape sequences and groups) to display them in color on the terminal. Colors are defined by the environment variable GREP_COLOR. The deprecated environment variable GREP_COLOR is still supported, but its setting has no priority. WHEN isnever, always orauto.

-L, –files-without-match

Usage: Instead of printing matching lines, print the filenames of files that do not contain the specified pattern. When using the -L option, grep will search for files that do not contain the specified pattern and then display only the file names of these files. The scanning process for each file stops after the first match is found. (-L is specified by POSIX.)

-l, –files-with-matches

Usage: Instead of printing matching lines, print the filenames of files containing the specified pattern. When the -l option is used, grep will search for files that do not contain the specified pattern and then display only the file names of these files. The scanning process for each file stops after the first match is found. (-l is specified by POSIX.)

-m NUM, –max-count=NUM

Usage: Stop reading the file after NUM matching lines. If the input is standard input from a regular file, and the output is NUM matching lines, grep will ensure that standard input is positioned after the last matching line before exiting, regardless of whether There are subsequent context lines. This enables the calling process to resume searching. When grep stops after NUM matching lines, it outputs any subsequent context lines. When using the -c or –count options, grep will not output counts greater than NUM. When using the -v or –invert-match options, grep outputs NUM unmatched lines then stop.

-o, –only-matching

Usage: Print only the matching (non-empty) parts of the matching line, each part on a separate output line.

-q, –quiet, –silent

Usage: Silence; do not write anything to standard output. If any matches are found, exit immediately with zero status, even if an error is detected. See also the -s or -no-messages options. (-q is specified by POSIX.)

-s, –no-messages

Usage: Disable error messages about non-existent or unreadable files. Portability note: Unlike GNU grep, version 7 of Unix grep is not POSIX compliant because it lacks -q and its The -s option behaves similarly to GNU grep‘s -q option. USG-style grep also does not have -q, but its -s option behaves like GNU grep. Portable shell scripts should avoid using -q and -s and instead redirect standard output and error output to /dev/null. (-s is specified by POSIX.)