Regular expression for command summary in CentOS

Source: Internet
Author: User
Regular expressions are classified into two types: basic regular expressions (BRE) and extended regular expressions (ERE ). Metacharacters of the basic regular expression: 1 ,.: match any single character 2, []: match a single character within the specified range 3, [^]: match a single character out of the specified range 4 ,*: match the characters 5 ,\? : Match the first 0 or 1 6 times

Regular expressions are classified into two types: basic regular expressions (BRE) and extended regular expressions (ERE ).

  1. The metacharacters of the basic regular expression:

    1.. match any single character

    2. []: match a single character in the specified range

    3. [^]: match a single character out of the specified range

    4. *: match any number of characters above it

    5 ,\? : Match the first 0 or 1 times

    6. \ +: match the first 1 or multiple times

    7. \ {m \}: Exact Match m times

    8. \ {m, n \}: match at least m times, at most n times

    9. *: match any character of any length

    10. ^: The first line is anchored,

    11. $: anchor at the end of the line

    12. \ <: specifies the beginning of a word (\ B can also be used)

    13. \>: The end of the word is anchored (\ B can also be used)

    14. ^ $: Match blank rows

    15. \ (\): used together with \ n (n is a number) to match the reproduction times of a single or multiple character combinations.

II. extend the metacharacters of the regular expression:

1.. match any single character

2. []: match a single character in the specified range

3. [^]: match a single character out of the specified range

4. *: match any number of characters above it

5 ,? : Match the first 0 or 1 times

6. +: match the first 1 or multiple times

7. {m}: Exact Match m times

8. {m, n}: match at least m times, up to n times

9. *: match any character of any length

10. ^: The first line is anchored,

11. $: anchor at the end of the line

12. \ <: specifies the beginning of a word (\ B can also be used)

13. \>: The end of the word is anchored (\ B can also be used)

14. ^ $: Match blank rows

15. (): used together with \ n (n is a number) to match the reproduction times of a single or multiple character combinations.

16. |: Match | character before or after a symbol

3. commands that can match regular expressions include: (here we only use the grep command)

1. grep: grep uses a basic regular expression to match text rows.

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

Grep [OPTIONS] [-e PATTERN |-f FILE] [FILE...]

Option:

-E: use an extended regular expression for matching (equivalent to egrep)

-F: equivalent to fgrep

-I: Case sensitivity is ignored during matching.

-V: displays rows in unmatched mode.

-O: only the matched rows are displayed.

-Q: Silent mode

-- Color = auto: highlight matched characters

IV. example:

1. display the rows ending with bash in the/etc/passwd file;

2. display two or three digits in the/etc/passwd file;

3. display the lines that end with 'listen' followed by 0, 1, or multiple white spaces in the 'netstat-tan' command results

4. add bash, testbash, basher, and nologin users (the shell of nologin users is/sbin/nologin), and find the rows in the/etc/passwd file with the same username as the shell name;

5. display the default shell and UID of the root, centos, or user1 user on the current system;

6. find a word in the/etc/rc. d/init. d/functions file (the word can contain underscores in the middle) followed by a group of parentheses.

7. use echo to output a path, and then egrep finds the base name of the path;

Further: use egrep to retrieve its directory name

8. find the number between 1-in the ifconfig command execution result;

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