Linux operations Practice-Regular expressions

Source: Internet
Author: User
Tags expression engine egrep

1. Summarize the use of the commands involved in this course and the relevant examples show

Mainly used for regular expressions, including basic regular expressions and extended regular expressions

2. Summarize basic regular expressions and extended regular expressions

The metacharacters of the basic regular expression:
Character Matching:
.: matches any single character;
[]: matches any single character within the specified range;
[^]: matches any single character in a non-specified range;

[: Lower:], [: Upper:], ...

Number of occurrences: used to specify the number of times after the character;
*: any time;

\?:0 or 1 times;

\+:1 or multiple times;
\{m\}: Precisely limited to M times;
\{m,n\}: 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 mode;
$: End of line anchoring; for the rightmost side of the pattern;
\< \b: The first anchor of the word; the left side of the pattern used to denote the word;
\> \b: the ending anchor; 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

stored in the built-in variables, these 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;
\2: What the pattern in the second parenthesis matches to

Back reference: Use a variable to refer to the character that matches the pattern in the preceding grouping brackets;

Extend the metacharacters of regular expressions:
Character Matching:
.: Any single character
[]: matches any single character within the specified range

[^]: matches any single character in a non-specified range

Number of matches:
*: Any number of times
?: 0 or 1 times;
+: more than 1 times;
{m}: exact match m times;
{M,n}: At least m times, up to n times;

^: Anchoring the beginning of the line
$: Anchor Line End
\<, \b: Anchor word first
\>, \b: Anchor ending

Group: ()
Back reference: \1, \2, ...

A|b,a or B

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

# grep '--color=auto ' bash$/etc/passwd

650) this.width=650; "title=" 1.png "alt=" wkiom1xjh1nih4o5aaebfnplosq110.jpg "src=" 72/6a/wkiom1xjh1nih4o5aaebfnplosq110.jpg "/>

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

Basic Regular Expression grep "\<[1-9][0-9]\{1,2\}\>"/etc/passwd

650) this.width=650; "title=" 2.png "alt=" wkiom1xjmbtxh5pjaah-si92ama952.jpg "src=" 72/6c/wkiom1xjmbtxh5pjaah-si92ama952.jpg "/>

Extended regular Expression Grep-e "\<[1-9][0-9]{1,2}\>"/etc/passwd

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

Basic Regular Expression Ss-ant | grep "listen\ (^$\) \{0,\}"

650) this.width=650; "title=" 3.png "alt=" wkiom1xjk5jrztvdaacsd1qjoni908.jpg "src=" 72/6b/wkiom1xjk5jrztvdaacsd1qjoni908.jpg "/>

Extended Regular Expression Ss-ant | Egrep "LISTEN (^$) {0,}"

6. Add user bash, Testbash, basher, and Nologin user (Nologin user's shell is/sbin/nologin);

Find the line in the/etc/passwd file that has the same user name as its shell name

Extended Regular expression: grep-e "^ ([A-za-z0-9]{1,}):. *\1$" passwd

650) this.width=650; "title=" 4.png "alt=" wkiol1xjozytxqakaadybjsoqsq784.jpg "src=" 72/68/wkiol1xjozytxqakaadybjsoqsq784.jpg "/>

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) "passwd | Cut-d:-F 1,3,7

650) this.width=650; "Width=" 726 "height=" "title=" 5.png "style=" WIDTH:697PX;HEIGHT:76PX; "alt=" Wkiol1xjqtszd_ Voaacc3ikfdja694.jpg "src=" Http:// "/>

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

Egrep "\<[a-z]+_? [A-z]+\ (\) "/etc/rc.d/init.d/functions

650) this.width=650; "Width=" 705 "height=" 278 "title=" 7.png "style=" width:585px;height:270px; "alt=" Wkiol1xj78rgueb5aaewnoilrak911.jpg "src=" Http:// "/>

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

Echo/etc/sysconfig/test | Egrep-o "^/.*"

Echo/etc/sysconfig/test | Egrep-o "[^/][a-za-z0-9]+$"

650) this.width=650; "title=" 9.png "src=" Http:// " alt= "Wkiol1xkutnsqknuaacz26y4f1q779.jpg"/>

10. Find out the number of 1-255 in the execution result of IP add command

IP Add | Egrep "\< ([1-9]) | ( [1-9] [0-9]) | (1[0-9]{2}) | (2[0-5]{2})) \> "

650) this.width=650; "Width=" 907 "height=" 142 "title=" 8.png "style=" width:601px;height:131px; "alt=" Wkiol1xj9chceefxaagqhdrnzq8567.jpg "src=" Http:// "/>

This article is from the "^ Fox tail $" blog, so be sure to keep this source

Linux operations Practice-Regular expressions

Related Article

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: 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.