Basic usage of Grep,egrep regular expressions in Linux

Source: Internet
Author: User
Tags control characters egrep

Regular expressions

I wrote the blog for the first time. The basic use of regular expressions has just been learned recently. Organized, and later used to review the search.

The basic description of regular expressions:

Basically, a regular expression is a pattern used to describe a certain amount of text, and the shorthand for regular expressions is the Regex full name regular Express.

The first regular expression is very important for Linux OPS engineers, which will accompany every operational engineer's career. Speaking of regular expressions so have to say grep and Egrep,grep full name is globally search for a Regular expression and print out of the line, the global search regular expression and print the meaning of matching results, good Now let's uncover the mystery of regular expressions together.

Common formats:

grep [option] "pattern" FILE

Common options:

--color indicates that the matching result is displayed in highlight color

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7D/3A/wKioL1bi3pqzLFj_AABSrJKkcZ0480.png "title=" 1.png " alt= "Wkiol1bi3pqzlfj_aabsrjkkcz0480.png"/>


-I: Indicates case insensitive

. : (decimal point) means any single character that matches any

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/3A/wKioL1bi4C7TEukhAABvCUbwO08594.png "title=" 2.png " alt= "Wkiol1bi4c7teukhaabvcubwo08594.png"/>

-V: Indicates everything except the displayed result (without the-V effect above)

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/3A/wKioL1bi4FTi5Q1AAABWktKadLY017.png "title=" 4.png " alt= "Wkiol1bi4fti5q1aaabwktkadly017.png"/>

-O: Indicates that only matching content is displayed

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7D/3A/wKioL1bi4MOiDZWcAABNBQXtHxM673.png "title=" 7.png " alt= "Wkiol1bi4moidzwcaabnbqxthxm673.png"/>

*: Indicates the character before the match any time

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/3A/wKioL1bi4ODxsbVtAABfDaBk84o495.png "title=" 5.png " alt= "Wkiol1bi4odxsbvtaabfdabk84o495.png"/>

. *: Indicates any character that matches any length

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7D/3B/wKiom1bi4GfgyVgLAABdDTc21t0927.png "title=" 6.png " alt= "Wkiom1bi4gfgyvglaabddtc21t0927.png"/>

-N is a number that shows the matched content and the n rows following it

Example:

-a 1, such as:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7D/3A/wKioL1bi4PuSXcDtAABaz4hphuU973.png "title=" 8.png " alt= "Wkiol1bi4pusxcdtaabaz4hphuu973.png"/>

-B N: = n is a number, showing the match to the content and the rows preceding it

Example:

-B 1, such as:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7D/3B/wKiom1bi4IXArETfAABaJonxK6o084.png "title=" 9.png " alt= "Wkiom1bi4ixaretfaabajonxk6o084.png"/>

-C-N is a number, showing the matched content and its n rows

Example:

-C 1, such as:

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/7D/3C/wKiom1bi4JHzX0cPAABfNk2lA3s995.png " Title= "10.png" alt= "Wkiom1bi4jhzx0cpaabfnk2la3s995.png"/>

Regular expression meta-characters:

^: Indicates the beginning of the anchor, format:

' ^pattern '

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7D/3A/wKioL1bi4SSwUTKVAABH60bOUTQ699.png "title=" 11.png "alt=" Wkiol1bi4sswutkvaabh60boutq699.png "/>

$: Indicates the beginning of the anchor, format:

' pattern$ '

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/7D/3C/wKiom1bi4KmwVaVoAAA69MmOpsg228.png " Title= "12.png" alt= "Wkiom1bi4kmwvavoaaa69mmopsg228.png"/>

Note: Special usage, matching whitespace behavior ' ^$ '

[]: Indicates any character in the match range

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7D/3C/wKiom1bi4LrQEDEOAAAtX1pAckU622.png "title=" 13.png "alt=" Wkiom1bi4lrqedeoaaatx1packu622.png "/>

[^]: Indicates any single content outside of the matching range

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7D/3C/wKiom1bi4MfQJLSTAAA2F-V59Tw605.png "title=" 14.png "alt=" Wkiom1bi4mfqjlstaaa2f-v59tw605.png "/>

Common Range Representation format:

[0-9]: means all numbers equal to [[:d Igit:]]

[A-Z]: Indicates that all lowercase letters are equal to [[: Lower:]]

[A-Z]: Indicates that all uppercase letters are equivalent to [[: Upper:]]

[A-za-z]: Indicates that all letters are equal to [[: Alpha:]]

[a-za-z0-9]: means that letters and numbers are equal to [[: Alnum:]]

[[: Graph:]]: denotes non-null characters (non-spaces, control characters)

[[: Cntrl:]]: Indicates control character

[[:p UNCT:]]: denotes punctuation

[[:p Rint:]]: Indicates non-null characters (including spaces)

[[: Space:]]: denotes whitespace characters

\? : Indicates a match before the character 0 or 1 times

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7D/3C/wKiom1bi4PuwkmW4AAA1Ehs6IUI517.png "title=" 15.png "alt=" Wkiom1bi4puwkmw4aaa1ehs6iui517.png "/>

\{m,n\}: Indicates that the character before the match is at least m times, up to N times

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/3C/wKiom1bi4QeCaor6AAA6aev98Ck154.png "title=" 16.png "alt=" Wkiom1bi4qecaor6aaa6aev98ck154.png "/>

\{0,n\}: Indicates that the character before the match is up to N times

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7D/3A/wKioL1bi4ZvSBuIXAABGTtX_V7w943.png "title=" 17.png "alt=" Wkiol1bi4zvsbuixaabgttx_v7w943.png "/>

\{m,\}: Indicates that the character before the match is at least n times

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7D/3C/wKiom1bi4SHjD4nMAAA137DFmOQ939.png "title=" 18.png "alt=" Wkiom1bi4shjd4nmaaa137dfmoq939.png "/>

\{m\}: Indicates that the character before the match is at least m times

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7D/3C/wKiom1bi4THj_McZAAAx0x3BQYA432.png "title=" 19.png "alt=" Wkiom1bi4thj_mczaaax0x3bqya432.png "/>

\<: Indicates the first anchor of the word, the same as the use of \b

\>: Indicates the word ending anchor, equivalent to the use of \b

such as:\<word\>: The anchor word, the equivalent of \bword\b personally feel \b easier haha

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/3A/wKioL1bi4sehuq81AABMX9yO7NE111.png "title=" < >.png "alt=" Wkiol1bi4sehuq81aabmx9yo7ne111.png "/>

Grouping and referencing:

\ (pattern\): Represents the character to which this PATTERN is matched as an inviolable whole

\ (pattern\). *\1: Indicates references to previously grouped content

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/3C/wKiom1bi4mvDk2gZAAA_edEkhnI237.png "title=" 20.png "alt=" Wkiom1bi4mvdk2gzaaa_edekhni237.png "/>

Egrep Meta-characters:

+: Indicates that the preceding character matches at least once

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7D/3C/wKiom1bi4nWy_Z-NAAA75VQADjw118.png "title=" 21.png "alt=" Wkiom1bi4nwy_z-naaa75vqadjw118.png "/>

| : The pipe character is represented here | on either side of the symbol

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/3C/wKiom1bi4oTT5hGlAABK9JSPkQY804.png "title=" 22.png "alt=" Wkiom1bi4ott5hglaabk9jspkqy804.png "/>

( | ) : With parentheses

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7D/3A/wKioL1bi4ymQ6NKQAAAzx9vyRvU376.png "title=" 23.png "alt=" Wkiol1bi4ymq6nkqaaazx9vyrvu376.png "/>

? : Equal to \ in grep?

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7D/3A/wKioL1bi4zXwwfb_AAA2ehwZvVQ897.png "title=" 24.png "alt=" Wkiol1bi4zxwwfb_aaa2ehwzvvq897.png "/>

(): equivalent to \ (\) in grep

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7D/3A/wKioL1bi40Ch6GtSAAA9utqWF0I950.png "title=" 25.png "alt=" Wkiol1bi40ch6gtsaaa9utqwf0i950.png "/>

{}: Equivalent to grep in \{\}

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7D/3C/wKiom1bi4sfDCF14AAAzgp7jsZE339.png "title=" 26.png "alt=" Wkiom1bi4sfdcf14aaazgp7jsze339.png "/>

Case:

    1. Show rows in Test.txt that do not end in CB

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7D/3A/wKioL1bi41zjTRFoAAA9GahWchM472.png "title=" 27.png "alt=" Wkiol1bi41zjtrfoaaa9gahwchm472.png "/>

2. Locate the three-bit or four-digit number in the test file

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/3A/wKioL1bi42ng6iysAAA6NNLvf7Q444.png "title=" 28.png "alt=" Wkiol1bi42ng6iysaaa6nnlvf7q444.png "/>

3. Locate the/etc/grub2.cfg file, starting with at least one whitespace character followed by a non-whitespace character line;

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7D/3C/wKiom1bi4vjSZjKDAAB9UAOiTsw442.png "title=" 29.png "alt=" Wkiom1bi4vjszjkdaab9uaoitsw442.png "/>


4. Find the result of "fdisk-l" command, including the line with/dev/followed by SD or HD and a small letter;

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/3A/wKioL1bi45PyAhYoAABQH5kPYP8907.png "title=" 32.png "alt=" Wkiol1bi45pyahyoaabqh5kpyp8907.png "/>

This article is from the Linux OPS blog, so be sure to keep this source http://lailai900201.blog.51cto.com/3094187/1750221

Basic usage of Grep,egrep regular expressions in Linux

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.