Home >php教程 >PHP开发 >grep linux command

grep linux command

高洛峰
高洛峰Original
2016-12-13 14:32:321438browse

grep (global search regular expression(RE) and print out the line, comprehensive search for regular expressions and print out the line) is a powerful text search tool that can use regular expressions to search text and print out matching lines print it out. The Unix grep family includes grep, egrep and fgrep. The egrep and fgrep commands are only slightly different from grep. egrep is an extension of grep, supporting more re metacharacters. fgrep is fixed grep or fast grep, which treats all letters as words. That is to say, the metacharacters in regular expressions represent their own literal meanings. , no longer special. Linux uses the GNU version of grep. It is more powerful and can use the functions of egrep and fgrep through the -G, -E, and -F command line options.

The way grep works is that it searches one or more files for a string template. If the template contains spaces, it must be quoted, and all strings following the template are treated as file names. The search results are sent to the screen without affecting the original file content.

grep can be used in shell scripts because grep indicates the status of the search by returning a status value, which is 0 if the template search is successful, 1 if the search is unsuccessful, and 2 if the file being searched does not exist. We can use these return values ​​to perform some automated text processing work.

grep --help
Match pattern selection:
-E, --extended-regexp Extended regular expression egrep
-F, --fixed-strings A collection of newline-delimited strings fgrep
-G, -- basic-regexp Basic regular expression
-P, --perl-regexp The perl regular expression called
-e, --regexp=PATTERN The following root regular pattern, the default is none
-f, --file=FILE Obtain the matching pattern from the file
-i, --ignore-case                                                                                          ’ ’ s ’ s ’ with ’ s ’ ’               ’ s ’ ’ s ’ s ’ s ’ ’ s to 1 byte---to match the entire word

Data lines, but not blank lines

Miscellaneous:
-s, --no-messages                                                             use using   using using ‐ ‐             through ‐ using ‐ ‐s, ​​                                             -                                                                 - -HELP display help information--mmap user memory-mapped input if public

Input control:
-M,-Max-count = num maximum number -b,-byte-questiont print matching The block number where this row is located.
-n, --line-number Displayed plus the line number of the match
--line-buffered Refresh output for each line
-H, --with-filename When searching for multiple files, display the matching file name prefix
-h, --no-filename When searching for multiple files, do not display matching file name prefixes
--label=LABEL print LABEL as filename for standard input
-o, --only-matching Only display matching PATTERN in one line Part of
-Q,-QUIET, --silent does not show anything
-binary-FILES = Type assumes of Type types of binary files;
Type can be `binary ',` text', or `without-match'
-a, --text Matches binary things
-I Does not match binary things
-d, --directories=ACTION Directory operations, read, recurse, skip
-D, --devices=ACTION Set the device, FIFO, pipeline operation, reading, skipping
-R, -r, --recursive Recursive call
--include=PATTERN Only find files matching FILE_PATTERN
--exclude=PATTERN Skip files and directories matching FILE_PATTERN
--exclude-from=FILE Skip all files except FILE
-L, --files-without-match When matching multiple files, display unmatched ones File name
-l, --files-with-matches When matching multiple files, display the matching file name
-c, --count               Show how many times it has been matched
  -Z, --null                                                     -                                                                                                                                                      

File control:
-B, --before-context=NUM Print the match itself and the previous lines controlled by NUM
-A, --after-context=NUM Print the match itself and the following lines controlled by NUM Control
-C, --context=NUM Print the match itself and, subsequently, the first few lines controlled by NUM
-NUM The usage of -C is the same as
--color[=WHEN],
--colour[=WHEN ] Use flags to highlight matching strings;

-U, --binary Use flags to highlight matching strings;
-u, --unix-byte-offsets When the CR character is not present, report byte offsets (MSDOS mode )


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn