Linux: the regular expressions used and matched by grep

Source: Internet
Author: User

POSIX character entity: [: entity name:]:

[: Alpha:], [: lower:], [: Upper:]: letter, lower case letter, upper case letter

[: Digit:], [: xdigit:]: decimal number, hexadecimal number

[: Alnum:]: letters, numbers

[: Punct:]: punctuation

[: Space:]: blank (space, tab, new line)

[: Graph:]: printable character, non-control character, not including spaces!

[: Print:]: printable character, non-control character, including space, [:], [:],

[: Cntrl:]: Control Character

 

Several modes:

-G basic regular expression mode: default mode! : ^, $ ,., *, [], [^], \ W, \ W, \ <, \>, \ (\), X \ {M \}, X \ {M, \}, X \ {M, N \}, \ B .. \ B

-E extension mode: supported: + ,? , |, (|), X {m}, X {M,}, X {m, n} extension Modes

-F fixed naming string mode:

-P Perl extension mode:

 

Several options:

-I case-insensitive:

-W word match, not character match: equivalent to the pattern '\ B searched word \ B'

-X-line Matching:

-V reverse matching:

 

Input parameter settings:

-E | -- Regexp: Specifies a regular expression.

-F | -- file: Specifies the file to be searched.

-R |-r | -- Recursive: round-robin processing of files in the directory

-- Include = file_pattern: Only search for the specified file

-- Exclude = file_pattern: exclude the specified file from search.

-- Exclude-from = file: search for a specified file based on the content of the file.

-- Exclude-Dir = dir_pattern: exclude the specified folder for search.

-C | -- count: print only the number of matched rows instead of the actual row content. Note: This option provides output for each input file. Can be used together-

Option to count the number of unmatched rows!

-L: only print the name of the file that matches the pattern.

-L: only prints the names of files that do not match the pattern.

-M n (n is a non-negative integer) | -- Max-count n (n is a non-negative integer): After processing n matches for each file, the subsequent processing is stopped.

 

 

 

How to handle binary files:

-- Binary-files = {Binary| Text | without-match}: the binary file is processed as a binary file by default. Grep prints a line of information, prompting whether the mode is matched!

-A | -- text | -- Binary-files = text: WhenCompositionProcess this file

-I | -- Binary-files = without-match: the binary file is considered as mismatched!

 

 

Output format settings:

-O | -- only-matching: only prints the string that matches the pattern.

-N | -- line-number: print the row number:

-H print file name: default action. You can use the "-h" option to cancel this action.

-- Color | -- color: The character pattern that is highlighted.

-A n (n is a non-negative integer): print the N rows after the matching mode at the same time.

-B n (n is a non-negative integer): print the N rows before the matching mode at the same time.

-C | -- Context n (n is a non-negative integer) |-n (n is a non-negative integer): print the N rows before and after the matched pattern.

-C | -- count: print only the number of matched rows instead of the actual row content. Note: This option provides output for each input file. Can be used together-

Option to count the number of unmatched rows!

-L: only print the name of the file that matches the pattern.

-L: only prints the names of files that do not match the pattern.

-S | -- no-messages: do not print error messages

-B,-u: print the offset byte characters of the matching string.

 

Related Article

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.