Simple usage of regular expressions and pipeline and redirection

Source: Internet
Author: User
Tags character set expression lowercase range regular expression

The regular expression (Regular EXPression), in simple terms, is the function of retrieving a string that conforms to some grammatical rules. Here we take grep as an example to learn regular expressions:

grep: Filters text commands using patterns defined by basic regular expressions;

Usage of grep:

grep [options] pattern [FILE ...]

Options

-I: Ignore character case

--color: Matching character display Color example: Alias Grep=grep--color)

-V: Shows rows that are not matched by a pattern

-O: Displays only strings that are matched to the pattern

-E: Using extended regular expressions

-A: Shows two lines behind the matching file

-B: Show two lines to the front of the matching file

-C: The contents of the two lines before and after the matching file are displayed

Regular expression:

Basic Regular Expressions

Extending regular Expressions

(The number of matches is in greedy mode, that is, the match is as long as possible.) )

Basic Regular Expressions

Metacharacters

.: Match any single character

# grep ' root. ' a.txt--> shows the line containing root in the A.txt file and followed by any character behind it

*: Matches the previous character any time (can be 0 times)

A*b--> B,ab,aab,aaab

# grep ' a*b ' test.txt

. *: Match any character of any length

A.*B:AB,AAB,AMNB. --> (the file contains a line ending with a b at the beginning of a)

\?: The characters in front of the match are 1 or 0 times (partially matched on the line)

\{m,n\}: Matches the preceding character at least m times, at most n times. (modifier first \ preceding character)

# grep ' a\{1,3\}b ' test.txt--> match A at least once 3 times

[]: matches any single character within the specified range

[0-9]: Take any number in 0-9

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

[^[0-9]]: any one character other than 0-9

Character Set combination

[: Space:]: blank characters

[:p UNCT:]: Punctuation

[: Lower:]: Small Letter

[: Upper:]: Capital letters

[: Alpha:]: Uppercase and lowercase letters

[:d Igit:]: Digital

[: Alnum:]: Numbers and uppercase and lowercase letters

Anchor of Position:

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

grep "^root"/etc/passwd--> line with root in/etc/passwd file

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

bash$--> the line with bash as the end of the line

^$: Blank Line

\< or \b: Anchor the first word, any character following it must appear as a word header

\<abab--> Abab There are characters behind it, generally used to specify that a string is not at the end of a line

\> or \b: anchoring the suffix, any character preceding it must appear as the tail of the word (IBID.)

Group: \ (\)

\ (ab\) *: AB can appear any time or 0 times. (Take AB as a whole)

Back reference: The string that appears before can be called

\ (\) ... \1..\2..\3

\ n: The nth opening parenthesis and all the contents of the corresponding closing parenthesis

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

--> in a/etc/inittab file, a line that begins with a number and ends with the same number

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.