My study record--grep, Egrep.

Source: Internet
Author: User

Concept:

grep (globally search a Regular expression and print) is a powerful text-search tool that can use regular expressions to search for text and print matching lines.

grep: A traditional grep program that supports basic regular expressions

Egrep: Extended grep program, supports extended regular expressions

Regular expressions: Simply put, use a string of your own defined conditional strings to match the characters in the document that meet the criteria.


grep basic Format:

grep [OPTIONS] PATTERN [FILE ...]

Common options:

-O: Show only what is matched to the pattern

-I: (Ignore case) does not distinguish between character casing when searching

-V: Shows rows that cannot be matched to a pattern

-E: Using extended regular expressions

-A #: Displays the line to which the pattern is matched and the next # line

-B #: Displays the lines that are matched by the pattern and the # lines above

-C #: Displays the top and bottom # lines that are matched by the pattern

--color[=when],--colour[=when]:

Used to set the match to the character, display the highlight color No, there are three options auto, always, never

For example:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5C/2C/wKiom1UbyYHAM-PcAAB5V84jxFc457.jpg "title=" QQ picture 20150401183233.png "alt=" Wkiom1ubyyham-pcaab5v84jxfc457.jpg "/>

Basic regular Expression meta-characters:


Character Matching:

. : Matches any single character

[]: Any single character within the specified range

[^]: Any single character outside the specified range


Number of matches: A control is provided after the expected match character, which is used to express the number of occurrences of the preceding character specified

*: Any length, indicated 0 times, 1 times or more;

. *: Any character of any length (works in greedy mode, matches as long as possible)

\?: matches its previous character 0 or 1 times, indicating that its left character is optional

\+: Matches the preceding character 1 or more times, indicating that its left character appears at least once

\{m\}: Exact Match of its left character appears m times

\{m,n\}: At least m times, up to N times:

\{0,n\}: Up to n times

\{m,\}: At least m times


Location anchoring:

^: Anchor beginning, Shape: ^pattern

$: Anchor line tail, shaped like:pattern$

^pattern$: Use pattern to match whole line

For example:

grep "^$" matches blank word closest (lines with spaces do not match)

grep "^[[:space:]]*$" matches all empty lines (can have spaces)

grep "^sh.*sh$"/etc/passwd matches the entire line in the/etc/passwd file, beginning with sh and ending with SH

Word anchoring: A continuous string consisting of non-special characters

\<: Anchor Word can also be used \b, shape such as: \<pattern \bpattern

For example: grep "\<sh"/etc/passwd matches the word "sh" in/etc/passwd file

\>: Anchor ending, can also be used \b, shaped like:pattern\> pattern\b

\<pattern\>: Matches the entire word PATTERN can match


Group matching:

\ (pattern\)

Note: The patterns in the grouping, the characters that are matched during a specific match, can be memorized by grep (stored in the built-in variables when the variables are \1,\2,... ), so that it can also be referenced

\1: Reference, left to right in the pattern, matched by the first opening parenthesis and the pattern in the right parenthesis corresponding to it

\2: Reference, pattern from left to right, matched by the second opening parenthesis and the pattern in the right parenthesis corresponding to it

For example:

grep "\ (a.b\). *\1" FILE: "A.b" appears in the matching file line, and the same string that the front "a.b" matches to appears again

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5C/27/wKioL1Ub0WGRkA_4AACoWibJEl4592.jpg "title=" 22.jpg "alt=" wkiol1ub0wgrka_4aacowibjel4592.jpg "/>

Amb Hello amb Matching

The Amb hello anb mismatch \1 matches the contents of the previous match to the Amb, not the \ (a.b\) mode



Extended Regular Expression metacharacters: (no special instructions are used and basic regular expressions are the same)

Character Matching:

. : Matches any single character

[]: Any single character within the specified range

[^]: Any single character outside the specified range

Number of matches:

*: Any time

?: 0 or 1 times

+: at least 1 times

{m}: exact match m times

{M,n}: At least m times, up to N times

{m,}: at least m times

{0,n}: up to n times

Location anchoring:

^: Beginning of the line

$: End of line

\<,\b: the first word

\>,\b: Ending

^$: Blank Line

Group:

(PATTERN): Reference \1,\2,...


Or: (All content on both sides)

A|b:a or B

abc| CBA:ABC or CBA


Egrep (GREP-E) command: (Basic and grep)

Grep-e PATTERN FILE ...

Egrep PATTERN FILE ...

Practice:

1. Display the lines in the/proc/meminfo file that begin with uppercase or lowercase s;

# grep-i "^s"/proc/meminfo--color=auto or;

# grep "^[ss]"/proc/meminfo--color=auto

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5C/27/wKioL1Ub0mDi-VCwAABYn_jaOBc509.jpg "title=" 33.jpg "alt=" Wkiol1ub0mdi-vcwaabyn_jaobc509.jpg "/>

Write with an extended regular expression:

# egrep-i "^s"/proc/meminfo--color=auto

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5C/31/wKiom1Ub1trzqjMKAABY7ZB6bws140.jpg "title=" 565. JPG "alt=" wkiom1ub1trzqjmkaaby7zb6bws140.jpg "/>

2. Display the user whose default shell is non-/sbin/nologin in the/etc/passwd file

# grep-v "nologin$"/etc/passwd;-v shows rows that are not matched to

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5C/28/wKioL1Ub0_6ifbTJAACzE_NMBp4740.jpg "title=" 44.jpg "alt=" Wkiol1ub0_6ifbtjaacze_nmbp4740.jpg "/>

Write with an extended regular expression:

# grep-e-V "nologin$"/etc/passwd;-e means using extended regular expressions

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5C/31/wKiom1Ub106i5gBHAACnAJGmL6k417.jpg "title=" 666. JPG "alt=" wkiom1ub106i5gbhaacnajgml6k417.jpg "/>


3. Identify the user whose user name and default shell are the same on the current system

# grep "^\ (. *\) \>.*\1$"--color=auto/etc/passwd

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/5C/2E/wKiom1Ub0_vj-7XIAADEVG1kv8U261.jpg "title=" Qq20150401191657.jpg "alt=" Wkiom1ub0_vj-7xiaadevg1kv8u261.jpg "/>

Write with an extended regular expression:

# grep "^ (. *) \>.*\1$"--color=auto/etc/passwd

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/5C/31/wKiom1Ub1_7jtfoAAADElo_hEL8683.jpg "title=" 77.jpg "alt=" Wkiom1ub1_7jtfoaaadelo_hel8683.jpg "/>




This article is from the "Linux" blog, so be sure to keep this source http://laoliao.blog.51cto.com/10027070/1627435

My study record--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.