Grep and grep commands

Source: Internet
Author: User

Grep and grep commands
Grep (Global search REgular expression and Print out the line) searches for FILE input named after a FILE, or standard input to find rows containing content that matches the specified PATTERN, by default, grep prints the rows containing Matching content. Grep has two variants: egrep and fgrep. egrep are equivalent to grep-E, and fgrep is equivalent to grep-F.Synopsis

Grep [Options]PATTERN[FILE...]
Grep [Options] [-EPATTERN|-FFILE] [FILE...]

Options
  • -A num: print the matching rows.Num row belowAnd separate adjacent matching groups --
  • -B num: print the matching rowThe preceding num rowAnd separate adjacent matching groups --
  • -C num: print the matching rowThe preceding and subsequent num rowsAnd separate adjacent matching groups --
  • -B prints the current row in the input file before the row matching the outputByte offset
  • -C will only printTotal number of matched rows, Used with-v, which is the total number of unmatched rows
  • -- Color = [color] matches the surrounding color. The color can beAlways, nerver, auto
  • -E: Extended Regular Expression
  • -F treats PATTERN as a fixed string list separated by a new line, as long as it matches one of them.
  • -P treats PATTERN as a Perl regular expression.
  • -G regards PATTERN as a basic expression. This isDefault
  • -F file: Obtain the mode from the file,One mode for each row, Empty file 0 Mode
  • -H: print the file name for each matching row
  • -H when you search for multiple files, you cannot add a file name before the output line.
  • -IIgnorePATTERN andCase sensitivityRespectively
  • -L disable the normal matching output. It will output the name of each input file that does not generate the output, and it will stop when the first match is encountered during file scanning.
  • -I disables the normal matching output, which only outputsName of the input file that generates the output. It is stopped when the first match is encountered during file scanning.
  • -M num does not read this file after finding num matching rows. If the input is a standard input from a common file and has been outputNumMatch rows. grep ensures that the standard input is located after the last matched row at exit, regardless of whether or not the following rows are output. In this way, a caller can resume searching. When grepNumAfter a matched row is stopped, it will output any followed rows. When the-c or -- count option is used, grep does not output a ratio.NUMMore rows. When the-v or -- invert-match option is specified, grep will outputNUMAnd then stop.
  • -N is added to each output line withRow number
  • -O only displays the parts matching the PATTERN in the matching row.
  • -QQuiet, No output. If any matching content is found, it immediately exits with the status value 0.
  • -R,-r recursively reads all files in the directory
  • -- Include = PATTERN: recursive search in the directory only when searching for files matching PATTERN
  • -- Exclude = recursive search by PATTERN in the directory, but the file matching PATTERN is skipped.
  • -V: print the grep version number.
  • -V: returns the opposite result. Only unmatched rows are selected.
Metacharacter of the basic Regular Expression
  • String Matching

. ------------- Match any single character
[] ------------- Match any single character in the specified range
[^] ------------- Match any single character in the specified range

[: Lower:] ------------- all lowercase letters

[: Upper:] ------------- all uppercase letters

[: Alpha:] ------------- all letters

[: Alnum:] ------------- all numbers and letters

[: Digit:] ------------- all numbers

[: Space:] ------------- all blank characters

[: Punct:] ------------- all punctuation marks

  • Matching times

* ------------- Any time

\? ------------- 0 or 1 time

\ + ------------- 1 or multiple times;
\ {M \} ------------- exact limit is MB
\ {M, n \} ----------- at least m times, at most n times, [m, n]
\ {0, n \} ----------- up to n times
\ {M, \} ----------- at least m times

  • Position anchoring

^ ------------- The first line is anchored to the leftmost part of the pattern.
$ ------------- Anchor at the end of the line, used for the rightmost of the Mode
\ <Or \ B ------------- the beginning of the word, used to represent the left side of the Word Pattern
\> Or \ B ------------- the end of the word, used to indicate the right side of the Word Pattern
^ $ ------------- Blank line

  • Group

()

  • Or

|

Fgrep

It is to regard all letters as words. That is to say, the metacharacters in regular expressions indicate that they return their literal meaning, and are no longer special.

Egrep

Egrep improves many traditional grep operations, such:
In grep? And + must be written \?, \ +, But egrep does not
Grep does not support a | B or (abc | xyz), but egrep does.
When grep matches x {n, m}, it must be processed with \ {and \}, while egrep does not.
... I only know so much about it. Please add it!

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.