Grep Regular Expression principles and application examples

Source: Internet
Author: User
Tags grep regular expression egrep

Application background: We just added a user Luffy, but we don't know what its default shell is.

Q: How do I retrieve a user's default shell?


Solution:

# Grep '^ Luffy \>'/etc/passwd | cut-D:-F7

/Bin/bashf

Through the above method, we get the default shell of the user Luffy.


Text Search Tool: it is used to match lines by line based on the text mode specified by the user, and finally obtain lines that conform to the text mode.

Grep is a powerful text search tool. The following describes grep Syntax:

Grep [Options] pattern [file...]

Grep is followed by the options, followed by the mode, and finally the file name


1,Its common options are::

-E, -- extended-Regexp // that is, extended grep. We will refer to egrep later.

-I, -- ignore-case // that is, case insensitive

-V, -- invert-match // that is, take the opposite operation

-O, -- only-matching //, that is, only the matched string is displayed, not the row

-B, -- before-context = num // that is, extract the data of the first three rows of the matched row.

-A, -- After-context = num // that is, extract the data of the last three rows of the matched row.

-C, -- Context = num // that is, extract the data of the upper and lower three rows of matched rows.

-N, -- line-number // that is, when the matching result is displayed, add the row number

2,Speaking of matching ModesWe will introduce the second concept,Regular Expression:

Regular Expression: A pattern composed of a type of characters. The characters in the pattern do not represent their literal meaning. It is a metacharacter that controls or supports wildcard functions.

In short, regular expressions are set to precisely describe the required content and can be used for multiple commands.


Metacharacters of the basic regular expression:

A. Character matching:

.: Match any single character

For example, L. E matches with L, followed by two characters, and finally contains e rows.

[]: Match any single character in the specified range

[0-9], [[: digit:] ---- match a number

[A-Z], [[: lower:] ---- match lowercase letters

[A-Z], [[: Upper:] ---- match uppercase letters

[[: Punct:] ---- match punctuation marks

[[: Alnum:] ---- match letters or numbers

[[: Alpha:] ---- match letters (uppercase and lowercase letters can be used)

[[: Space:] ---- match Spaces

[^] ----- Reverse Query

Example: [^ A-Z] match any character out of the range A to Z

B. Number of times matching metacharacters: the number of times the previous character appears:

*: Indicates that the first character can appear any time.

\? : Indicates that the character before it can appear 0 times or 1 time, that is, the character is dispensable

Example: lover \? That is to say, R is dispensable and can make lover or love

\ {M \}: exact match, indicating that the first character must appear m times

\ {M, N \}: range match, indicating that the first character appears between m and n. The minimum is m and the maximum is N.

\ {M ,\}:: range match, indicating that the first character appears at least m times

\ {0, n \}: range match, indicating that the first character can appear up to n times and can be matched between 0 and N times.

C. positioning:

^: The beginning of the line, that is, the start position of the line.

Example: ^ love matches all rows starting with love

$: The end of a row, that is, the end of a row.

Example: Love $ matches all rows ending with love

^ $: The beginning and end of a row, indicating a blank row

\ <: Specifies the beginning of a word. You can use \ B to match the left of a word.

Example: \ <love matches a line that contains a word beginning with love

\>: The end of the word. You can use \ B to match the right of the word.

Example: love \> match the line containing the word ending with love

D. GROUP:

\ (\): The content matched by the mode in the group, which can be remembered by the regular expression in the memory and can be used later.

E. Reference:

\ N: reference the Matching content of the parentheses instead of the pattern itself.

For example, \ (love \). * \ 1R matches love, then references it, adds R to it, and changes it to lover.


Extended Regular Expression (egrep): grep-E = egrep

Character match:

.: Match any single character

[]: Match any single character in the specified range (apply the above basic regular expression)

[^]: Returns the inverse.

Matching times: (unlike the above regular expressions, there is no escape character \)

*: Any time

? : 0 or 1 time

+: At least once

{M}: exact match m times

{M, n}: At least m times, up to n times

{M ,}: minimum MB

{0, n}: up to n times

Anchoring: (refer to the above basic regular expression usage)

GROUP:

(): The content matched by the mode in the group, which can be stored in the memory by regular expressions and can be used later.

Note: Unlike the above regular expressions, there is no escape character.

Reference: Same as the basic regular expression.

Or: |

RedHat | centos: RedHat or centos

Instance application (basic ):

Preparation: operations will be performed on the file named chsang.txt.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3C/17/wKioL1PA0XeBOz_fAABsAI212eM064.jpg "Title =" 1.jpg" alt = "wkiol1pa0xeboz_faabsai212em064.jpg"/>

Grep:

1. display a line with one character between B and Y

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3C/17/wKiom1PA0fDC2ZLcAAAvgEYab0U587.jpg "Title =" 2.jpg" alt = "wkiom1pa0fdc2zlcaaavgeyab0u587.jpg"/>

2. Find the rows starting with lowercase s:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3C/17/wKioL1PA0hKgMfOtAAAy-XnZnvY649.jpg "Title =" 3.jpg" alt = "wKioL1PA0hKgMfOtAAAy-XnZnvY649.jpg"/>

3. Find the rows starting with a blank space

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3C/17/wKioL1PA0lGhU5DhAAA56iCGEKU552.jpg "Title =" 4.jpg" alt = "wkiol1pa0lghu5dhaaa56icgeku552.jpg"/>

4. Find the rows starting with a number.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3C/17/wKioL1PA0uKgeQZBAAAz_NFimAI040.jpg "Title =" 5.jpg" alt = "wkiol1pa0ukgeqzbaaaz_nfimai040.jpg"/>

5. Find the rows with most and must words.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3C/18/wKiom1PA00ySlZTEAAAzWt6LWks450.jpg "Title =" 6.jpg" alt = "wkiom1pa00yslzteaaazwt6lwks450.jpg"/>

6. Find the rows starting with small and smll.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3C/17/wKioL1PA03DAmCDFAAA2aBkOkNo237.jpg "Title =" 7.jpg" alt = "wkiol1pa03damcdfaaa2abkokno237.jpg"/>

7. Find the row with R at the beginning of a word.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3C/18/wKiom1PA0-_BSYnMAAA1O0ENqVE552.jpg "Title =" 9.jpg" alt = "wKiom1PA0-_BSYnMAAA1O0ENqVE552.jpg"/>

Egrep:

1. Find the rows starting with uppercase/lowercase S.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3C/18/wKiom1PA1E_R70VcAAA46oojfPA057.jpg "Title =" 8.jpg" alt = "wkiom1pa1e_r70vcaaa46oojfpa057.jpg"/>

2. Only the words ending with a vertex are displayed.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3C/18/wKiom1PA1iDSe2DKAABAX6A842k901.jpg "Title =" 10.jpg" alt = "wkiom1pa1idse2dkaabax6a842k901.jpg"/>

3. Find rows with two or more digits.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3C/18/wKioL1PA1kySqy07AAA2yOmQ5Qc905.jpg "Title =" 11.jpg" alt = "wkiol1pa1kysqy07aaa2yomq5qc905.jpg"/>

Raise question:

1. Retrieve the IP address of the current host

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3C/19/wKiom1PA2AmQLZAcAAA3JIs-aDE986.jpg "Title =" 12.jpg" alt = "wKiom1PA2AmQLZAcAAA3JIs-aDE986.jpg"/>

2. Extract the lines starting with # and followed by a space in the/etc/inittab file.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3C/19/wKioL1PA2O3wa9U7AADed2dZT3A349.jpg "Title =" 13.jpg" alt = "wkiol1pa2o3wa9u7aaded2dzt3a349.jpg"/>

3. Find the number between 1-In the ifconfig command result.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3C/19/wKiom1PA2cKguepbAACxnaaR8kw620.jpg "Title =" 15.jpg" alt = "wkiom1pa2ckguepbaacxnaar8kw620.jpg"/>


Summary: in processing data, we can use regular expressions to retrieve the data we need from the file and display it.

If you do not know anything, you may want to use man to check it or Google it.


All of the above are my personal understandings after learning. Here I would like to thank marco education for its growth. At the same time, please contact me for any mistakes in this article, [email protected]. This is my first blog. Thank you!

This article is from the blog "the child of the ox cross is naked" and will not be reproduced!

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.