Linux_grep and regular expressions

Source: Internet
Author: User

linux-basic-grep and regular expressions

Text Lookup command:


grep: Searches text based on patterns and displays lines of text that conform to the pattern

Pattern: The matching criteria for the combination of meta-characters of the expression wildcards regular the text character

grep [option] PATTERN [FILE ...]

650) this.width=650; "src=" "title=" 1.png " alt= "Wkiol1xj7udhovyraacocw3adhe244.jpg"/>

To find the contents of a file that contains the root string

Parameters for grep

-I: Indicates ignoring case

650) this.width=650; "src=" "title=" 2.png " alt= "Wkiom1xj6zyy6jqbaabm8krafng389.jpg"/>

The case of the H letter is ignored in the

--color: The matching option is highlighted with highlighting

650) this.width=650; "src=" "title=" 3.png " alt= "Wkiol1xj7xgifitwaabtxlpctfm515.jpg"/>

-V: Reverse lookup, rows that are matched to are not displayed, rows that do not match are displayed

650) this.width=650; "src=" "title=" 4.png " alt= "Wkiom1xj63razqpqaacegsi4sqq939.jpg"/>

-O: Displays only the strings that are matched, one row for each string

650) this.width=650; "src=" "title=" 5.png " alt= "Wkiol1xj7a7ixbmnaaayiqw7qgk278.jpg"/>

-W: Absolute match

650) this.width=650; "src=" "title=" 100. PNG "alt=" wkiol1xj7ixjcfinaabvnuiqbne955.jpg "/>


The following are the definitions in the wildcard characters:

*: Any character of any length

? : Any single character

[]: Within the specified range

[^]: outside the specified range

These definitions have been changed in the meta-character, note the distinction between


Regular Expressions: Regular expression


.: Any single character

650) this.width=650; "src=" "title=" 6.1. PNG "alt=" wkiol1xj7fpanffwaabumn3tnuk277.jpg "/>

Represents any two single characters between R and T, and all r/ft on the way are matched.

[]: matches any single character within the specified range

[^]: matches any single character outside the specified range

650) this.width=650; "src=" "title=" 7.jpg " alt= "Wkiom1xj7bzdle7zaabhz8vxkzi874.jpg"/>

The above character sets correspond to each other: numeric lowercase letters, uppercase punctuation, whitespace characters, all letters, all numbers and characters, these references need to be used in addition [], two []

Number of characters

650) this.width=650; "src=" "title=" 8.jpg " Style= "Float:none;" alt= "wkiol1xj7lman1lwaaaw30tnrvo806.jpg"/>

*: Match the characters in front of it Fuzhin Italian Times

For example, a*b means that there is an arbitrary number of a in front of B ( instead of A and B can have any character, A and B can have any character is a wildcard representation, not a meta-character representation ), that is, can match the preceding 0 a, 1 a,ab , the first 2 A,aab, and so on, so in addition to the number one and the last three, the other can be matched to.

650) this.width=650; "src=" "title=" 9.png " Style= "Float:none;" alt= "wkiom1xj7dqyo_reaaavwfi35y8982.jpg"/>

650) this.width=650; "src=" "title=" 10.png "Style=" Float:none; "alt=" wkiol1xj7lngc8upaaa2usortds577.jpg "/>650" this.width=650; "src=" Wyfs02/m00/72/6d/wkiol1xj7lnxhs4paaa73zq-uom665.jpg "title=" 11.png "style=" Float:none; "alt=" Wkiol1xj7lnxhs4paaa73zq-uom665.jpg "/>

All red is the matching success.

. *: denotes arbitrary length of any character, greedy mode, as long as possible to match

650) this.width=650; "src=" "style=" float: none; "title=" 12.jpg "alt=" Wkiom1xj7kewebq6aaaozlx8awq722.jpg "/>

The a.*b means a begins, B ends, and any character in the middle

650) this.width=650; "src=" "style=" float: none; "title=" 12.png "alt=" Wkiol1xj7sbx4qaiaabd_un1dis586.jpg "/>

650) this.width=650; "src=" "style=" float: none; "title=" 13.png "alt=" Wkiol1xj7wps2z5taaa5k5ujqfs029.jpg "/>

650) this.width=650; "src=" "style=" float: none; "title=" 14.png "alt=" Wkiol1xj7wprrmmaaaa10mpdn5e049.jpg "/>

Unable to match to SSADDB

It can be understood that the second and subsequent values in front are dead, must exist, and that the first character in front can be any number, which can be 0.

\?: Match its preceding character 1 or 0 times

650) this.width=650; "src=" "title=" 15.png "Style=" Float:none; "alt=" wkiom1xj7osg-zo4aaau8d9tslu451.jpg "/>

AaB cannot be fully matched

\{m,n\}: Matches its preceding character at least m times, more than n times

650) this.width=650; "src=" "style=" float: none; "title=" 16.png "alt=" Wkiom1xj7qai1tv2aaa-fbz0aoo587.jpg "/>

\{1,\}: Indicates at least once, up to No limit

650) this.width=650; "src=" "title=" 17.png "Style=" Float:none; "alt=" wkiol1xj7yxju-abaaayykj9r2s186.jpg "/>

Indicates that a minimum of 1 to 3 characters can occur between A and B

Location anchoring:

^: Anchor the beginning of the line, any content after this character must appear at the beginning of the line

650) this.width=650; "src=" "title=" 18.png "Style=" Float:none; "alt=" wkiom1xj7qfhhxvraacfjsdl_ji670.jpg "/>

$: Anchor Line end. Any content preceding this character must appear at the end of the line

650) this.width=650; "src=" "title=" 19.png "Style=" Float:none; "alt=" wkiol1xj7ywh6--qaab2limhnx4400.jpg "/>

^$: Blank Line

650) this.width=650; "src=" "title=" 20.png "Style=" Float:none; "alt=" wkiom1xj7qecocmiaaaki8eww7w770.jpg "/>

^#$: Represents a row that matches a line that starts with #, with empty contents in the middle

650) this.width=650; "src=" "style=" float: none; "title=" 21.png "alt=" Wkiol1xj76pibc9eaacxrsmujzu597.jpg "/>

650) this.width=650; "src=" "style=" float: none; "title=" 22.jpg "alt=" Wkiom1xj7yxb1xxwaaboqsrxea8379.jpg "/>

Represents the content that matches a number ending with a white space character

\< or \b: Anchor word, any character following it must appear as the first word

650) this.width=650; "src=" "title=" 23.png "Style=" Float:none; "alt=" wkiol1xj76oieu4xaab3xbhzmke966.jpg "/>

\> or \b: anchors the ending, any character in front of which must appear as the tail of the word

650) this.width=650; "src=" "style=" float: none; "title=" 24.png "alt=" Wkiom1xj7yxjb0njaabuolusoxe102.jpg "/>

Not counted as part of the tail of the word

\<root\>: Exact Match root word

650) this.width=650; "src=" "title=" 25.png "Style=" Float:none; "alt=" wkiol1xj76pyynfnaaazzt2m6gm593.jpg "/>



\ (ab\) *: This means AB can appear 0 or any time, back reference

650) this.width=650; "src=" "style=" float: none; "title=" 26.jpg "alt=" Wkiol1xj8grqqnreaabbqtaufia669.jpg "/>

650) this.width=650; "src=" "style=" float: none; "title=" 27.jpg "alt=" Wkiom1xj7kzzemxgaacnuv6sswa126.jpg "/>

This article is from the "It_landscape" blog, make sure to keep this source

Linux_grep and regular expressions

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: 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.