grep and regular expressions

Source: Internet
Author: User


grep: Search for text according to pattern, and display text that conforms to the pattern

Pattern: A matching condition in which the literal character wildcards regular a combination of the meta-characters of the expression.

Single quotation marks: Strong references


Double quotation marks: weak references

-I: Ignore case


--color: Match to result plus color

Alias grep= ' grep--color '

-V: Show rows that are not matched by the pattern

-O: Displays only the string to which the pattern matches

-E: Using extended regular expressions

-A N: Shows the rows to match and the N rows following the row

-B N: Shows the rows to match and the N rows in front of the row

-C N: Shows the rows to match and the N rows after the line

*: Any character of any length

?: any single character

Regular Expressions: REGular expreesion, REGEXP


Metacharacters

. : Match any single of characters

[]: matches any single character within the specified range

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

[:d Igit:], [: Lower:], [: Upper:],[:space:]


Number of matches (greedy mode):

*: Matches any of its preceding characters any time

A, B,ab,aab, ACB, ADB, AMNB

A*B:B, AB, AaB

A.*b:ab, AAB, ACB, ADB, AMNB

. *: Any character of any length


\?: Match its preceding character 1 or 0 times

\{m,n\}: Matches the preceding character at least m times, up to N times

\{0,3\}


Location anchoring:

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

$: Anchor line end, any content after this character must appear at the end of the row

^$: Blank Line


\< or \b: Any subsequent character must appear as a word header

\> or \b: Any character in front of it must appear as the tail of the word

\<root\> or \broot\b: Exact match


Group:

\(\)

\ (ab\) *: AB as a whole, appears one or more times

1\: Refers to the first opening parenthesis and the corresponding closing parenthesis to match the contents of the

2\:


Exercise: Analyze the characteristics of the first two lines in the following text in the/etc/inittab file, and write a pattern that can be precisely similar to the current one.

11:1:WAIT:/ETC/RC.D/RC 1

13:3:WAIT:/ETC/RC.D/RC 3

grep ' ^1\ ([0-9]\): 1\.* 1\$ '/etc/inittab



To extend the regular expression:


Character Matching:

.

[]

[^]


Number of matches:

*:

?:

+: Match its preceding character at least 1 times

{M,n}

Location anchoring:

^

$

\<

\>


Group:

(): Group

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


Or:

| : OR

C|cat: Match C or cat

(C|c) at: Match cat or cat



























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