grep Regular Expression

Source: Internet
Author: User
Tags grep regular expression

Need for Text lookup:

Grep:global research, based on the pattern, searches for text and displays lines of text that conform to the pattern.

Egrep,fgrep

Pattern: Patterns in English, the text word wildcards regular the expression of the meta-character combination to form a matching condition.

grep [Optians] [pattern] File ...

-I--ignore-case ignoring case

--color

-V Reverse lookup rows that are matched by a pattern are not displayed

-O displays only strings that are matched by the pattern

*: Any character of any length

? : Any single character

[]: Within the specified range

[^]: outside the specified range


Regular expression: Regular expression,regexp

grep ' pattern ' file

Metacharacters

.: matches any single character;

[]: matches any single character of the specified range

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

[: lower:],[:upper:],[:p unct:],[:space:],[:alpha:],[:alnum:],[:d igit:]

Number of matches:

*: Matches any of its preceding characters any time

A, B, AB, AAB,ACB,ADB,AMNB

A*b

A.*b

. *: Any character of any length

\? : Make a match, match its preceding character once or 0 times

Normal work in greedy mode, as long as possible to match

\{m,n\}: Matches its first character at least m times, up to N times

\{1,\} at least once

\{0,3\} up to three times

grep ' A\{1,3\}b A

Location anchoring:

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

grep ' ^r. T '/etc/passwd

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

grep ' y$ '/etc/inittab

grep ' B. h$ '/etc/passwd

^$: Blank Line

Anchor Word head:

\<: Any character following it must be a word, the first appears, the anchor word

\>: Any character in front of it must appear as a word, trailing, anchor ending

\<root\>

\broot\b =\<root\>, can use B instead of <,>

Group:

\(\)

\ (ab\) * AB can appear 0 times, 1 times, any time

grep ' \ (L.. e\). *\ (L.. e\) ' A matches with L. E, and the middle is any character


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

[2]

[3]

grep ' \ ([0-9]\). A number appears in the *\1$/etc/inittab line and ends with the same number

grep ' \ (^[0-9]\). *\1$/etc/passwd with first number of switches and ending with the same number

Exercise: Analyzing the characteristics of the first two lines in the following file in the/etc/inittab file (the number must be the same in each row), write a pattern that can be found exactly like two lines:

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

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


grep ' ^1\ ([0-9]\). \1.*\1$ '

Regular Expressions:

Basic REGEXP: Basics

Extended REGEXP: Extended

Basic Regular Expressions:

.: Any single character

[]: Specify Range

[^]: outside the specified range

Number of matches:

*: any time before it

\?:0 Times or 1 times

\{m,n\)}: At least m times, to more than n times

. *: Characters of any length

Anchoring:

^: Beginning of the line

$: End of line

\<,\b: Any subsequent word must appear in the header

\>,\b: Any word in front must appear in the tail

\ (\) for grouping

\1,\2,\3 ... for a back reference

grep: A command that filters text using patterns defined by a basic regular expression

-I.

-V Reverse search, mismatched display

-O displays only the matched string

--color

-E: Using extended regular expressions

-a# (numeric): When a row is matched by a pattern specified by grep,

Grep-a 2 ' ^core ID '/proc/cpuinfo followed by 2 lines

-B:

Grep-b 2 ' ^core ID '/proc/cpuinfo Front 2 lines

2 rows before and after the-C:-C2

To extend the regular expression:

Character matching: Same as basic

.

[]

[^]

Number of Matches

*: Same as basic

? : Its previous or 0 times

+: Its preceding character at least once, matching its preceding character at least 1 times

+* approximate *

{M,n}, no need to use \, use directly

Position anchoring is the same as the basic

Group:

(): Group

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

|: Or: The meaning of a|b or

grep--color-e C|cat A.txt

Egrep ' \b ([0-9]|[ 0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-9]\b '

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