20150827-grep and regular Expression learning

Source: Internet
Author: User
Tags expression engine egrep

Summary of the use of grep and examples of how to display

Basic usage: grep is used as a text search tool to search the target text row by line based on user-specified filters (written by metacharacters and text characters of the regular expression) and display the matching rows

Format: grep [OPTIONS] PATTERN [FILE ...] :

Common options:

--color=auto: The matching to the string to do highlighting;
-V: The display mode does not match the row;
-I: ignore character case;
-O: Displays only strings that can be matched to the pattern;
-Q: Silent mode;
-E: Using extended regular expressions;

The metacharacters of the basic regular expression:

Character Matching:

.: Matches any single character

[]: matches any single character of the specified range

[^]: does not match any character in the specified range

[: Lower:]: Matching lowercase letters

[: Upper:]: Matching uppercase letters

[[:d Igit:]]: Match numbers

[[: Alnum:]]: Match numbers and uppercase and lowercase letters

[[: Space:]]: matches all whitespace characters

[[:p UNCT:]]: Match punctuation

Number of occurrences: used after the characters that need to be specified;

*: Indicates that the preceding character can appear any time;

\?: Indicates that the preceding character appears 0 or 1 times;

\+: at least once;

\{m\}: The exact number of occurrences is m times;

\{m,n\}: The preceding character at least m times up to n times [M,n]

\{0,n\}: Up to n times;
\{m,\}: at least m times;

. *: Matches any character of any length;

Location anchoring:

^: Anchor at the beginning of the line; for the leftmost side of the pattern (for example, the row is bash: ^bash)

$: End of line anchoring; for the rightmost side of the pattern (for example, the end of the line is bash: bash$)

\< \b: The first anchor of the field; the left side of the pattern used to represent the word;
\> \b: The end of the field anchors; the right side of the pattern used to represent the word;
^$: blank line;

Group: \ (\)

The contents of the pattern in the grouped parentheses are recorded by the regular expression engine during execution, and the built-in variables are saved: The variables are \1, \2, ...
\1: From the left, the first opening parenthesis, and the matching right parenthesis in the middle of the pattern match to the content;
Back reference: Use a variable to refer to the character that matches the pattern in the preceding \b's grouping brackets;

Exercises:

3. Display the lines in the/etc/passwd file that end with bash

grep ' bash$ '/etc/passwd

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/72/88/ Wkiom1xlfimsmktmaabz2z86gpw694.jpg "" 444 "height=" "/>

4. Display the two-digit or three-digit number in the/etc/passwd file

Grep–color=auto ' \b[[:d igit:]]\{2,3\}\b '/etc/passwd

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/72/88/ Wkiom1xlfjchor1daahzfq5-eha980.jpg "" 461 "height=" 286 "/>

5. Display lines with ' LISTEN ' followed by 0, one or more whitespace characters in the ' Netstat-tan ' command result

Netstat–tan | grep ' listen[[:space:]]*$ '

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/72/84/ Wkiol1xlgfjxfhzjaadzsmhexds665.jpg "" 541 "height=" 288 "/>

6. Add user bash, Testbash, basher, and Nologin user (Nologin user's shell is/sbin/nologin), then find the same line in the/etc/passwd file as the user name and its shell name

Useradd bash.  Useradd Testbash. Useradd basher. Useradd-s/sbin/nologin Nologin

grep ' ^\ (\b[[:alnum:]]\+\b\). *\1$ '/etc/passwd

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/72/88/ Wkiom1xlfjrz9mltaaet4ymokjw167.jpg "" 606 "height=" 179 "/>

7. Display the default shell and UID of root, CentOS, or User1 user on the current system (please create these users beforehand, if not present)

grep-E ‘^root|^centos|^user1‘ /etc/passwd|cut-d: -f1,3,7

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" Http://s3.51cto.com/wyfs02/M02/72/88/wKiom1Xlfj6D7gWDAABtyYo_ Wvi968.jpg "" 628 "height="/>

8, find a word in the/etc/rc.d/init.d/functions file (the middle of the word can be underlined) followed by a set of parentheses line

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/72/88/ Wkiom1xlfkeqi-d1aafzcah9rqu990.jpg "" 658 "height=" 369 "/>

9, use echo to output a path, and then egrep find its path base name; Further use Egrep to remove its directory name

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M01/72/88/ Wkiom1xlfkjw8uhbaabdm4v51c8468.jpg "" 593 "height="/>

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M02/72/88/ Wkiom1xlfkuivs1eaaa9narlwss047.jpg "" 597 "height="/>

10. Find the number between 1-255 in the result of ifconfig command execution

650) this.width=650; "title=" image "style=" border-top:0px; border-right:0px; Background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" image "src=" http://s3.51cto.com/wyfs02/M00/72/84/wKioL1XlgG3Qq_ Xlaac3vvaqrg4242.jpg "" 541 "height=" 103 "/>

20150827-grep and regular Expression learning

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.