grep, Egrep, and corresponding regular expression usages

Source: Internet
Author: User
Tags uppercase character egrep


The Grep,egrep command in a Linux system is a powerful text search tool that uses regular expressions and extended regular expressions to search for text and print the rows that match.


    1. Search for a specific string "the" Note: N is the display line number

# grep-n ' the ' regular_express.txt


2. Use the- v option to reverse the search for a specific string "the"

# grep-vn ' the ' regular_express.txt


3. Match the string with the case "the", using the-i option

# grep-in ' the ' regular_express.txt


4.[] Indicates a string matching "[]" in the range

When I search for the two words of test or tast, I find that they have a common ' t?st ', so we can search for them.

# grep-n ' t[ae]st ' regular_express.tx

Note: uppercase English/lowercase English/numbers can be written using [a-z]/[a-z]/[0-9], or can be written together

[A-za-z0-9] indicates that a string is required to be numeric and English

If we want to get the line with numbers, then:

# grep-n ' [0-9] ' regular_express.txt

can also be used

[[: Lower:]] instead of a-Z

[[:d Igit:]] instead of 0-9

[[: Upper:]]: all uppercase letters;

[[: Lower:]]: all lowercase letters;

[[:d Igit:]]: all numbers;

[[: Alpha:]]: all letters;

[[: Space:]]: white space characters;

[[:p UNCT:]]: punctuation;

Example: # grep-n ' [^[:lower:]]oo ' Regular_express.txt

# grep-n ' [[:d igit:]] ' regular_express.txt, etc.


5.[^] Represents a string outside the range of "[]", such as [^0-9] for non-numeric characters, [^a-z] for non-uppercase character ranges

If we want to get the line without numbers, then:

# grep-n ' [0-9] ' regular_express.txt


6.^word represents a matching string (word) at the beginning of the line

Displays the string at the beginning of ' the '

# grep-n ' ^the ' regular_express.txt

Displays the beginning of the line as lowercase characters

# grep-n ' ^[a-z] ' regular_express.txt


7.word$ represents a matching string (word) at the end of a row

Displays the end of the line as a point. The line

# grep-n ' \.$ ' regular_express.txt

Show Blank Lines

# grep-n ' ^$ ' regular_express.txt


8.. sign to match 1 arbitrary characters

Find out g?? D string, beginning G four string ending D

# Grep-n ' G.. d ' Regular_express.txt


9. * Indicates a match of 0 to infinity (regular expression) before one character

"Goo*g" stands for Gog,goog,gooog ... such as

# grep-n ' Goo*g ' regular_express.txt


10. \ = Matches the preceding character 0 or 1 times

Match the O character 0 or one time

# grep-n Ro\?t Regular_express.txt


11. \+ matches the preceding character at least once

Match a string that contains at least one O character

Grep-n ro\+t Regular_express.txt


Note: the. symbol represents any character,. * represents an empty character or one to n any character

Find a string with two g in the middle containing any character

# grep-n ' G.*g ' regular_express.txt


\ denotes an escape character, preceded by a special character, will remove the meaning of the original special character

Note: {} because there is a special meaning in the shell, you need to add the signifier \ to make it meaningless.


\{m,n\} means finding the first character of N to M (regular expression)

Find a string with two O

# grep-n ' o\{2\} ' regular_express.txt

Find the string after G that contains 2 to 5 O and ends with a G

# grep-n ' Go\{2,5\}g ' regular_express.txt


\{n,\} represents more than n previous re characters

Find the string after g that contains more than 2 O and ends with G

# grep-n ' Go\{2,\}g ' regular_express.txt


\< or \b Indicates the first anchor of the word.

Find the string with the "RO" string at the beginning of the word

# grep-n "ro\<" Regular_express.txt

\> or \b indicates a final anchor

Find the string at the end of the word with the "ro" string

# grep-n "\>ro" Regular_express.txt


Egrep supports the use of extended regular expressions, which are basically the same as grep usage, except that the egrep is in addition to the final and final anchors

"<" ">" symbol to use "\" to change the meaning, the other symbols are not used,

Example:

1. + represents repeating one or more of the previous re characters

Example: Egrep ' Go+d ' regular_express.txt

Expression Search (God) (good) (Goood) ... And so on string, o+ stands for [more than one o]

2.? Represents a repeat of 0 or one of the previous re characters

Example: Egrep ' Go?d ' regular_express.txt

Represents a search (GD) string, o represents [empty or 1 O]


3. () indicates finding the group string

Example: Egrep ' G (la|oo) d ' regular_express.txt

Represents a search (glad) or (good) string


4. () + indicates identification of multiple repeating groups

Example: Echo ' axyzxyzxyzxyzxyzc ' |egrep ' A (xyz) +c '

Indicates that the search begins with a ending is C, with more than one ' xyz ' string in the middle


The rest of the usage is not a detailed example that mimics grep usage:


Range Matching:

The. Number means matching any single character

[]: matches any single character within the range

[^]: matches any single character outside the range

Number of matches:

*: Matches any time

?: matches 0 or 1 times

+: Match 1 or more times

{m}: match matches m times

{M,n}: matches at least m times, up to N times

{0,n}: matches at least 0 times up to unlimited times

{m,}: matches at least m times

Location Matching:

^ Indicates the beginning of the line anchoring

$ indicates end of line anchoring

\< or \b Indicates the first anchor of the word

\> or \b indicates a final anchor

This article is from the "Linux" blog, so be sure to keep this source http://wyg11.blog.51cto.com/11253863/1750068

grep, Egrep, and corresponding regular expression usages

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.