Regular expression grep egrep

Source: Internet
Author: User
Tags echo command expression engine egrep

Regular expression: Regual expression,regexp


A pattern written by a class of special characters and text characters, where some characters do not represent their literal meanings, but are used to denote control or the function of a wildcard.

Divided into two categories:

Basic Regular Expressions: BRE

Extended Regular expression: ERE


Grep:global search REgular expression and print out of the line

Function: The text Search tool, according to the user-specified "mode (filter)" to match the target text line by row to check, print the matching line.

Pattern: The filter condition written by the metacharacters and text characters of the regular expression.


Regular expression engine:


grep [options] pattern [file]

grep [Options]-E pattern [-f file] [file ...]


-I: Ignore character case

-O: Show only the string that matches to itself

-V: Shows rows that cannot be matched to

-E: Support for using extended regular expressions

-W: The whole line matches a word (one word can be repeated on a line)

-Q: Silent mode, do not output any messages

-R: Recursively find each directory

-C: Count rows

-E: Specifies a string as a template style for finding the contents of a file

-A #: After after # lines

-B #: before Ago # line

-C #: # lines before and after the context

-N: Match to line plus line number


Basic regular Expression meta-characters:

Character Matching:

. : Any single character

[]: matches any single character of the specified range

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


Number of matches:

Used after the character that you want to specify the number of occurrences. Used to limit the number of occurrences of the preceding character, which works by default in greedy mode.

*: matches the preceding character any number of times: 0, 1, multiple times

. *: Matches any character of any length

\? : matches the preceding character 0 or 1 times, that is, the preceding character is optional

\+: Matches its preceding character 1 or more times

\{m\}: Matches the preceding character m times

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

\{0,n\}: Up to n times less than or equal to n

\{m,\}: At least m times greater than or equal to M


Location anchoring:

^: Anchor at the beginning of the line for the leftmost mode

$: End of line anchoring

^...$: Match whole line

^$: Blank line ^[[:space:]]$ lines that contain white space characters

\< or \b: The first anchor of the word

\> or \b: Final anchoring

\<pattern\>: Anchoring complete words


Grouping and referencing:

\ (\) Escapes multibyte parentheses, bundling multiple characters together as a whole

The contents of the pattern in the grouping brackets are automatically recorded in the internal variables by the regular expression engine, which are:

\1: First set of parentheses (first parenthesis from left)

\2: Second set of parentheses

Cases:

grep "\ (L.. e\). *\1 "Love.txt



Egrep

Support for extended regular expression implementations similar to grep text filtering,-G supports general regular expressions

Egrep [OPTIONS] PATTERN [FILE ...]


Extend the metacharacters of regular expressions:

. : Any single character

[]: matches any single character of the specified range

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


Number of matches:

Used after the character that you want to specify the number of occurrences. Used to limit the number of occurrences of the preceding character, which works by default in greedy mode.

*: matches the preceding character any number of times: 0, 1, multiple times

? : matches the preceding character 0 or 1 times, that is, the preceding character is optional

+: Match its preceding character 1 or more times

{m}: matches the preceding character m times

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

{0,n}: up to n times less than or equal to n

{m,}: At least m times greater than or equal to M


Location anchoring:

^: Anchor at the beginning of the line for the leftmost mode

$: End of line anchoring

\< or \b: The first anchor of the word

\> or \b: Final anchoring


Grouping and referencing, with grep


Or:

A|b cat| C means the entire left or the right side.



Practice:


Find all the lines in the/proc/meminfo file that begin with uppercase or lowercase s, at least three ways:

Grep-i "^s"/proc/meminfo

grep "^[ss]"/proc/meminfo

Grep-e "^ (s| S) "/proc/meminfo


Displays information about Roo, CentOS, or user users on the current system

Grep-e "^ (root/centos/user) \>"/etc/passwd


Find the line at the beginning of the/etc/rc.d/init.d/functions file that follows a word (including an underscore) followed by a parenthesis

GREP-E-O "[_[:alnum:]]+\ (\)"/etc/rc.d/init.d/functions


Use the echo command to output an absolute path, using Egrep to remove the base name

echo/etc/sysconfig/| GREP-E-O "[^/]+/?$"


Find the value between 1-255 in the ifconfig command result

Ifconfig | GREP-E-O "\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \> "


Find all IPV4 addresses in the ifconfig command result

Ifconfig | GREP-E-O "(\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \>\.) (\< ([0-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \>\.) {2}\< ([0-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \> "


Find the row for the user name and shell name in the/etc/passwd file

Grep-e "^ ([^:]+\>). *\1$"/etc/passwd


Find the IP address of the ifconfig eth0

Ifconfig eth0 | grep "inet addr" | Cut-d:-F 2|cut-d ""-F 1



This article is from the "zebra930" blog, make sure to keep this source http://zebra930.blog.51cto.com/11736340/1834673

Regular expression grep egrep

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.