grep Regular Expression

Source: Internet
Author: User
Tags grep regular expression expression engine egrep


What is grep

grep full name is the Global seach REgular Expressionand Print out of the line. (full search regular expression and display), is a powerful text search tool, based on the user's text mode (search criteria) to the target file line-by-row search, and display can match to (or do not match) the line or text.

since grep is a full-fledged search for regular expressions and displays, what is the regular expression?

Regular expressions

Regular Expressions, (English: RegularExpression, which is often abbreviated as regex,regexp, or RE in code,is a pattern written by a class of characters, many of which do not denote word meaning, It is a function of expressing control or distribution.

Regular expressions are made up of ordinary character and metacharacters characters, so-called meta-characters do not represent their literal meanings, but are described with additional functionality. The search capability of regular expressions is implemented by programs, so the tools that handle regular expressions are called regular expression engines. Many languages support regular forms, but the way they are supported is slightly different.

Regular expressions are divided into basic regular expressions and extended regular expressions, different extended positive expressions support a little more,grep supports basic regular expressions,egrep (GREP-E) supports extended regular expressions, and fgrep Regular expressions are not supported.

Basic Regular Expressions ( grep )

The grammar grid test:

Grep[options] ... PATTERN [FILE ...]

Meta-character of a basic regular expression

1, character matching:

.: matches any single character

[]: matches any single character within the specified range

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

[0-9],[[:d Igit:]]: matches a single number

[A-z], [[: Lower:]]: matches a single small letter

[A-z],[[:upper:]]: matches a single large letter

[A-za-z],[[:alpha:]]: matches a single case-insensitive letter

[0-9a-za-z],[[:alnum:]]: matches a simple number or case-insensitive letter

[[: Space:]]: matches a single white space character, including spaces,tab

[[:p UNCT:]]: match a single punctuation mark

2, number of times match metacharacters: The number of occurrences of the character that is used to achieve the specified preceding

*: Any length, the characters in front of it can appear any time

\? :0 or 1 times, the characters in front of it are available in a meta-

\{m\}: The characters in front of it are now m Times

\{m,n\}: It appears before the character at least m times, up to n times

\{m,\}: It appears before the character at least m times

\{0,n\}: The characters that appear before it are up to n times

. *: Matches any character of any length

\+: It appears before the character more than 1 times

3, Position anchoring:

^: Anchor at the beginning of the line: write to the left of the pattern

$: End of line anchoring: Writing on the right side of the pattern

^$: Blank line

\<,\b: The first anchor of the word, appearing on the left side of the word

\>,\b: Ending anchor, appearing on the right side of the word

Note: Consecutive characters that do not contain special characters are composed of words called

4, grouping

\ (\): For example \ (bash\)

patterns in a grouping match to content that can be remembered in memory by the regular expression engine and then referenced

5, reference

\#: Reference to the preceding # opening parenthesis and the match to the pattern in the closing parenthesis that matches it

\| : For example,a\|b a or b

extend the regular expression ( Egrep )

syntax Format:

Egrep[option] ... ' PATTERN ' FILE ...

Grep-e

To extend the regular expression meta-character:

1, character matching

. : Matches any single character

[]: matches any single character within the specified range

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

2, number of matches

*: Any length, the characters in front of it can appear any time

? :0 or 1 times, the characters in front of it are available in a meta-

+: It appears before the character more than 1 times

{m} : The characters in front of it are now m Times

{M,n} : It appears before the character at least m times, up to n times

{m,} : The character before it appears at least m times

{0,n} : The characters that appear before it are up to n times

3, Position anchoring:

^: Anchor at the beginning of the line: write to the left of the pattern

$: End of line anchoring: Writing on the right side of the pattern

^$: Blank line

\< \b: The first anchor of the word, appearing on the left side of the word

\>, \b: Final anchor, appears on the right side of the word

4, grouping

(): Group

| : or

5, reference:

\#: Reference to the preceding # opening parenthesis and the match to the pattern in the closing parenthesis that matches it

grep, egrep command Options

-O: Displays only the matched string, not the line where the string is located

-V: Reverse selection

-I:ignore-case, ignoring character case

-A:-A # that shows the lines that match to and shows the lines below the line that match to

-B:-B #, displays the rows that match to, and shows the lines that match to the line above

-C:-C #, displays the rows that match to, and shows the rows that match to the top and bottom # lines

--color=auto: Match to content, default in red

Example:

1, displays the user whose default shell is non- /sbin/nologin in the/etc/passwd file

# grep "/sbin/nologin"-v/etc/passwd | Cut-d:-f1

650) this.width=650; "Width=" 554 "height=" 146 "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>

2, displays the user whose default shell is /bin/bash in the /etc/passwd file, showing only the user with the largest ID number in the above results

# grep '/bin/bash '/etc/passwd | Sort-t:-k3-n | Tail-1 |cut-d:-f1

650) this.width=650; "Width=" 554 "height=" "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ( "/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>

3. Find One or two numbers in the/etc/passwd file

# grep-e--color "\<[0-9]{1,2}\>"/etc/passwd

650) this.width=650; "Width=" 554 "height=" 345 "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>

4, display the/etc/rc.d/rc.sysinit file, start with #, followed by at least one white space character , and then followed by at least one non-whitespace character

# grep-e "^#[[:space:]]+[^[:space:]]+"/etc/rc.d/rc.sysinit--color

650) this.width=650; "Width=" 554 "height=" 272 "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>

5. Identify the userwhose user name and default shell are the same on the current system

# grep-e--color "^ (\<[[:alpha:]]+[[:alnum:]]*\>). *\1$"/etc/passwd

650) this.width=650, "width=" 554 "height=", "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ( "/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>

6. Find A word followed by a set of parentheses "()" lines in the/etc/rc.d/init.d/functions file :

# grep-e--color-o "\<[[:alpha:]]+\>\ (\)"/etc/rc.d/init.d/functions

650) this.width=650; "Width=" 553 "height=" 174 "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>

7, use the echo command to output a path, and then use grep to remove its base name

echo "/etc/sysconfig/network" |grep-o-E "[^/]+/?$" | Cut-d/-F1

650) this.width=650; "Width=" 553 "height=" "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ( "/e/u261/themes/default/images/word.gif") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>

8, write a pattern, can match the reasonable IPv4 address (1.0.0.1-239.255.255.254)

Ifconfig | Grep-o-E "([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-3][0-9]) (\. ( [0-9]| [1-9] [0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])) {2}\. ([1-9]| [1-9] [0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4]) "

650) this.width=650; "Width=" 553 "height=" "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ( "/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>


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.