grep command and regular expression

Source: Internet
Author: User

grep and regular Expressions

    1. Grep

      Searches for text based on the pattern (the combination of metacharacters of the text Word wildcards regular the expression) and displays the line of text that conforms to the pattern.

1.1grep Family:

grep : Commands used by basic regular Expressions

Egrep: commands used to extend regular expressions

Fgrep ( Fast grep ): Regular expressions are not supported and are highly efficient in strings that do not have regular expressions. Cup Low resource consumption

1.2grep command Format

grep [Options] PATTERN [FILE ...]

1.2.1option

- I. Ignore Case

--color : Adds color to the matched character

- V: show rows that are not matched by a pattern

- o : Displays only strings that are matched by the pattern

- E: when using extended regular expressions, Grep–e=egrep

-A #: show the following lines

- B #: show the previous line

- C #: show front and back lines

like : Display, /proc/cpuinfo the following content

, a[

-B:

C:

2. Regular Expressions:REGular expression, REGEXP

Basicregexp : Basic

Extendedregexp : Extended

2.1 Basic Regular Expressions

2.1.1 Character Matching

.: match any single character

[]: matches any single character within the specified range

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

Character set:[:d igit:], [: Lower:], [: Upper:], [:p unct:],[:space:], [: Alpha:], [: alnum:]

A.b

[]:

[^]:

2.1.2 number of matches (greedy mode):

*: match any of its preceding characters any time       

       .*: Any character of any length (any a beginning and with b end of character)

\?: match the characters in front of it 1 Times or 0 Times

\{m,n\}: match the characters in front of it at least m times, at most N times, when the maximum number of times is Unlimited N can be omitted, m the minimum value is 0 and can not be omitted.

\{1,\} : At least once

\{0,3\} : Maximum 3 Times

2.1.3 Location Anchoring:

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

$: Anchor Line end, any content preceding this character must appear at the end of the row

^$: Blank Line

Indicates no blank line

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

\> or \b: anchor ending, any character preceding it must appear as the tail of the word

Match a word exactly

Group:

\(\)

\ (ab\) *

back to reference

\ 1: refers to the first opening parenthesis and all the contents of the corresponding closing parenthesis

\ 2: refers to the second opening parenthesis and all the contents of the corresponding closing parenthesis

\ 3: refers to the third opening parenthesis and all the contents of the corresponding closing parenthesis

2.2 Expand Regular Expressions

2.2.1 character matching: (As with the basic regular expression)

.

[]

[^]

2.2.2 number of matches:

*: ( same as basic regular expression )

?: (and basic regular expressions are less \ )

+: match the characters in front of it at least 1 Times

Match at least one white space character

{m,n} ( and basic regular expressions less \)

2.2.3 Location Anchoring:

^

$

\<

\>

(As with the basic regular expression)

Group:

(): Group

\1, \2, \3, ... \ n

or :

|: OR

C|cat:cat or Cat ? C or cat(correct)


grep command and regular expression

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.