Basic regular Expressions for Linux (grep)

Source: Internet
Author: User

* * Regular expression: regual expression, shorthand 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 wildcard functions:

Divided into two categories:

Basic Regular Expressions: BRE

Extended Regular expression: ERE


Metacharacters: \ (hello[[:space:]]\+\) \+


Grep:global search Regulae Expresslon and Print out of the line


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

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


Regular expression engine:


grep [Options] PATTERN [FILE ...]

grep [Options] [-E PATTERN |-f file] [FILE ...]

OPTIONS:

--color=auto: Color The matching text to highlight:

-i:ignorecase, ignoring the case of characters:

-O: Displays only the string that matches to itself:

-V,--invert-match: Displays the rows that cannot be matched by the pattern:

-E,--extended-regexp: supports the use of extended regular expression meta-characters:

-Q,--quiet,--silent: Silent mode, which does not output any information



-a#:after, after # line

-b#: Before, Front # line

-c#:context, front and back # lines



Basic regular Expression meta-characters:

Character Matching:

.: Matches any single character:

[]: matches any single character within the specified range:

[^]: matches any single character outside of the established range

[:d igit:], [: Lower:], [: Upper:], [: Alpah:], [: Alunum:], [:p UNCT:], [: Space:]


Number of matches: the number of occurrences of the preceding character of the limiter, after the character to specify the number of occurrences: Default work vs. Greedy mode:

*: Match its preceding characters any time: 0, 1 times:

For example: grep "X*y"

. *: Matches any character of any length

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

\+: Matches the preceding character 1 or more times, and the preceding character appears at least once

\{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:

\{m,\}: At least m times:


Location Anchoring:

^: Beginning of line anchoring: used to write to the leftmost side of the pattern:

$: End of line anchoring: used to write to the far right of the pattern:

^pattern$: Use PATTERN to match whole line "

^$: blank line:

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


Word: A contiguous string (string) of non-special characters is called a word:

\< or \b: The first anchor of the word, used for the left side of the word pattern:

\> or \b: The ending anchor for the right side of the word pattern:

\<pattern\>: Match full word:


Grouping and referencing:

\ (\): Binds one or more characters together and treats them as a whole:

\ (xy\) *ab

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

\ 1: The pattern from the left side, the first opening parenthesis and the matching closing parenthesis, matches the character of the pattern to

\ 2: The pattern from the left side, the second opening parenthesis, and the matching closing parenthesis to the character that matches the pattern

\3

...

He loves his lover.

He likes his lover.

She likes ker liker.

She loves her liker


~] #grep "\ (L.. e). *\1 "Lovers.txt


Back reference: The character that references the pattern of the preceding grouping brackets to:





Example:

1. Displays the lines in the/etc/passwd file that do not end with/bin/bash:

~] #grep-V "Bin/bash"/etc/passwd

2. Find the two-digit or three-digit number in the/etc/passwd file;

~] #grep "\<[0-9]\{2,3\}\>"/etc/passwd

3. Locate the/etc/rc.d/rc.sysinit or/etc/grub2.cfg. A line in a file that starts with at least one whitespace character and is followed by a non-whitespace character:

~] #grep "^[[:space:]]\+[^[:space:]"/etc/grub2.cfg

4. Find the line that ends with ' LISTEN ' followed by 0,1 or more whitespace characters in the ' Netstat-tan ' command

Netstat-tan | grep "listen[[:space:]]*$"



Basic regular Expressions for Linux (grep)

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.