Linux shell script programming regular expression _linux shell

Source: Internet
Author: User
Tags lowercase

A regular expression and wildcard characters

1 A regular expression is a string that matches the criteria in a file, and a positive one contains a match, and grep,awk,sed commands can support regular expressions

The 2 wildcard is used to match the qualifying file name, wildcard is an exact match, LS,FIND,CP these commands do not support regular expressions, so you can only use the shell's own wildcard character to match.

Regular expression of two foundations

This refers to the test text of the brother company

1 * The previous character matches 0 times or any number of times

grep "*" Test_rule.txt

Match all content, including blank lines (since * can be matched 0 times)

grep "aa*" Test_rule.txt

Match contains at least one line of a

grep "aaa*" Test_rule.txt

Match a line with at least two a

2. Matches any character except a newline character

grep "s.. D "Test_rule.txt

Matches s and D directly must have a two-character line

grep "S.*d" Test_rule.txt

Match S and D direct arbitrary characters

grep ". *" Test_rule.txt

Match all content

3 ^ Match beginning $ match end of line

grep "^m" Test_rule.txt

Match lines that start with capital M

grep "n$" Test_rule.txt

Matches a line ending in lowercase n

Grep-n "^$" Test_rule.txt

Match Blank Line

4 [] matches any one of the characters specified in parentheses, matching only one character

grep "S[ao]id" Test_rule.txt

Match between S and I letters, either a, or O's line

grep "[0-9]" test_rule.txt

Match any one number

grep "^[a-z]" test_rule.txt

Match a line that starts with a lowercase letter

5 [^] matches any one character except in parentheses

grep "^[^a-z]" test_rule.txt

Match lines that start without lowercase letters

grep "^[^a-za-z]" test_rule.txt

Matches a line that starts without a character

6 "\" Escape character

grep ". $" Test_rule.txt

The line that matches to the end of.

7 {n} indicates that the preceding character appears n times

grep "A{3}" Test_rule.txt

Matches a row in which the letter a appears 3 consecutive times

grep "[0-9]{3}" Test_rule.txt

Match rows that contain 3 consecutive digits

8 {N,} indicates that the preceding character appears to be no less than n times

grep "^[0-9]{3,}" Test_rule.txt

Match lines beginning with at least 3 consecutive digits

9 {N,m} indicates that the preceding character appears no less than n times, up to M times

grep "Sa{1,3}i" Test_rule.txt

Matches between S and I at least 1 A, up to 3 a

OK, Linux shell programming-regular expression summed up here, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.