N-Day Learn the help command for a Linux command: grep

Source: Internet
Author: User
Tags glob perl regular expression

Objective
Any system will have problems, the problem is generally how to troubleshoot the bug? This time the log of the Exception Log and key node logs is very important, in the face of a lot of log files, how to find the useful information we need? In Linux, you can use the grep command to find this command, which is very powerful and is one of the commands I use most when troubleshooting online errors.


Command name
Grep


Use
Finds content within the specified file that contains the specified keyword (regular expression), matching by behavior unit


Use formatting
grep [OPTIONS] PATTERN [FILE ...]


Common options
-V (Show command version)

Regular pattern matching version
-E,--extended-regexp (interpret PATTERN as an extended regular expression)
-F,--fixed-strings (interpret PATTERN as a list of fixed strings, separated by newlines, any of which are to be matched.)
-G,--basic-regexp (interpret PATTERN as a basic regular expression (BRE, see below). This is the default.)
-P,--perl-regexp (interpret PATTERN as a Perl regular expression.)

Matching mode control
-E pattern (use pattern as the pattern. This can is used to specify multiple search patterns.)
-F file,--file=file (www.qixoo.qixoo.com obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing.)
-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.)
-X,--line-regexp (whole row matching)

Content Output Control
-C,--count (show only the total number of eligible)
--color[=when]
(Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and Sepa Rators (for fields and groups of context lines)
With escape sequences to display them in color on the terminal. The colors is defined by the environment variable grep_colors. When are never, always, or auto.)
-L,--files-without-match (output only unmatched list of file names print the name of each input file from which no output would normally has been PR inted)
-L,--files-with-matches (only the list of matching file names is output)
-M NUM,--max-count=num (a file only outputs the number of rows that match the criteria <=num)
-O,--only-matching (output only matching parts)
-Q,--quiet,--silent (do not output any content)
-S,--no-messages (suppress error messages about nonexistent or unreadable files.)

Prefix content output control
-B,--byte-offset
(Print the 0-based byte offset within the input file before each line of output. If-o (--only-matching) is specified, print the offset of the matching part itself.)
-H,--with-filename (output file name)
-H,--no-filename (does not output file name)
--label=label
(Display input actually coming from the standard input as input coming the from file LABEL.) This is especially useful if implementing tools like zgrep, e.g., GZIP-CD foo.gz |
grep--label=foo-h something. See also the-h option.)
-N,--line-number (show line number)
-T,--initial-tab (the contents of each section of the row are tab-terminated, such as: The File name tab line number tab content)
-U,--unix-byte-offsets
(Report Unix-style byte offsets. This switch causes grep to report byte offsets as if the file were a unix-style text file, i.e., with CR characters Stripp Ed off. This
Would produce results identical to running grep in a Unix machine. This option has a effect unless-b option is also used; It has no effect on platforms other than MS-DOS
and Ms-windows.)
-Z,--null
(Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name. For example, Grep-lz outputs a zero-byte after each file name
Instead of the usual newline. This option makes the output unambiguous, even in the presence of file names containing unusual characters like newlines. This option can be
Used with commands like Find-print0, perl-0, Sort-z, and xargs-0 to process arbitrary file names, even those that cont Ain newline characters.)

Context Line output control
-A NUM,--after-context=num
(Print NUM lines of trailing context after matching lines. Places a line containing a group separator (described under--group-separator) between contiguous groups of
Matches. With The-o or--only-matching option, this have no effect and a warning is given.)
-B NUM,--before-context=num
(Print NUM lines of leading context before matching lines. Places a line containing a group separator (described under--group-separator) between contiguous groups of
Matches. With The-o or--only-matching option, this have no effect and a warning is given.)
-C NUM,-num,--context=num
(Print NUM lines of output context. Places a line containing a group separator (described under--group-separator) between contiguous groups of matches. With The-o or
--only-matching option, this have no effect and a warning is given.)
--GROUP-SEPARATOR=SEP (use SEP as a group separator. By default SEP is double hyphen (--).)
--no-group-separator (use empty string as a group separator.)

File and directory settings
-A,--text (Find all files in this is equivalent to the--binary-files=text option.)
--binary-files=type
(If the first few bytes of a file indicate that the file contains binary data, assume this file is of type type.) By default, TYPE is binary, and grep normally outputs
Either a one-line message saying a binary file matches, or no message if there is no match. If TYPE is Without-match, grep assumes-a binary file does not match;
This was equivalent to The-i option. If TYPE is text, grep processes a binary file as if it were text; This was equivalent to the-a option. Warning:grep
--binary-files=text might output binary garbage, which can has nasty side effects if the output is a terminal and if the Terminal driver interprets some of it as commands.)
-D ACTION,--devices=action
(If an input file was a device, FIFO or socket, use ACTION to process it.) By default, the ACTION is read, which means this devices is read just as if they were ordinary files.
If ACTION is skip, devices is silently skipped.)
-D ACTION,--directories=action
(If an input file was a directory, use ACTION to process it.) By default, the ACTION is read, i.e., read directories just as if they were ordinary files. If ACTION is Skip,
Silently skip directories. If ACTION is recurse, read all files under each directory, recursively, following symbolic links only if they be on the C Ommand line. This is
Equivalent to The-r option.)
--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-from=file
(Skip files whose base name matches any of the File-name globs read from file (using wildcard matching as described under --exclude).)
--exclude-dir=dir (Exclude directories matching the pattern dir from recursive searches.)
-I (Process a binary file as if it does not contain matching data; This is equivalent to the--binary-files=without-match o Ption.)
--include=glob (Search only files whose base name matches GLOB (using wildcard matching as described under--exclude).)
-R,--recursive
(Read all files under each directory, recursively, following symbolic links only if they is on the command line. This was equivalent to the-d recurse option.)
-R,--dereference-recursive (Read all files under each directory, recursively. Follow all symbolic links, unlike-r.)

Other options
--line-buffered (use line buffering on output. This can cause a performance penalty.)
-U,--binary
(Treat the file (s) as binary.) By default, under MS-dos and Ms-windows, grep guesses the file type by looking at the contents of the first 32KB read from The file. If grep
Decides the file is a text file, it strips the CR characters from the original file contents (to make regular expressions With ^ and $ work correctly). Specifying-u
Overrules this guesswork, causing all files to is read and passed to the matching mechanism verbatim; If the file is a text file with cr/lf pairs at the end of each line,
This would cause some regular expressions to fail. This option have no effect on platforms other than MS-dos and ms-windows.)
-Z,--null-data
(Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the-z or--null option, this option can be used
With commands like Sort-z to process arbitrary file names.)


Practice
1. Find all content in a directory that contains keyword1 or keyword2
Grep-r-E ' keyword1 '-e ' keyword2 ' $dirname

2. Find the content that contains the Keyword1 and the file name suffix with. log
grep ' Keyword1 ' *.log

3. Find content that contains keyword1 display with line number
Grep-n ' Keyword1 ' $file

4. Find contains keyword1 output does not show file name
Grep-h ' keyword1 './*


Postscript
Some configuration items are not very understanding, may not have found the use of the scene, encountered the use of the scene, then carefully pondering >_<


Resources
"1" man grep

N-Day Learn the help command for a Linux command: grep

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.