[Shell] Regular expressions and wildcard characters

Source: Internet
Author: User

-------------------------------------------------------------------------------------

Regular expressions and wildcard characters:

1. The regular expression is used to match the qualifying string in the file , and the Regex contains the match . grep, awk, sed, and other commands can support regular expressions.

2. Wildcards are used to match the eligible file names , and the 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.

Basic Regular expression:

* Previous character matches 0 or more times

. Match any character except the line break

^ matches the beginning of the line. ^ABC matches lines that begin with ABC

$ matches the end of the line. abc$ matches lines that end with ABC

[] matches any one of the characters specified in the brackets, matching only one character. [Aeiou] matches any vowel letter, [0-9][a-z] matches a lowercase letter and a two-bit character that consists of one digit

[^] matches any character except for the characters in brackets

\ escape character. Used to cancel the meaning of a special symbol

\{n\} matches the characters in front of it exactly n times. [0-9]\{2\} 4 digits

\{n,\} matches the preceding character occurrences of no less than n times.

\{n,m\} matches the characters that precede it at least n times and appears at most m times. [a-z]\{6,8\} matches lowercase letters from 6 to 8 bits

Attention:

* represents any character in a wildcard, in which the previous character matches 0 or more times in a regular expression.

grep "A *" Exp.txt # matches all content, including line breaks (just because it can match 0 times)

grep "aa*" Exp.txt # matches a row with at least one a

grep "aaa*" Exp.txt # matches a string that contains at least two consecutive a

grep "A.. E "Exp.txt # matches words with two characters between A and E

grep "S.*e" Exp.txt # matches words with any character between A and E

grep ". *" Exp.txt # matches all content

grep "^m" Exp.txt # matches lines starting with M

grep "n$" Exp.txt # matches rows ending with n

grep "^$" Exp.txt-n # matches all blank lines,-n can display line numbers

grep "^[a-z]" Exp.txt # matches a line beginning with a lowercase letter

grep "A[bc]e" Exp.txt # matches Abe or ace

grep "^[^a-z]" Exp.txt # matches lines that do not start with a lowercase letter

grep "[^a-za-z]" Exp.txt # matches lines with characters other than letters

grep "\.$" Exp.txt # matches lines ending in.

grep "A\{2\}" Exp.txt # matches a row that appears two times consecutively

grep "[0-9]\{2,\}" Exp.txt # matches a row with two digits in succession

grep "ab\{1,3\}" Exp.txt # matches a row with 1-3 B after

Link:http://www.cnblogs.com/farwish/p/4804012.html

@ Black eyed poet <www.farwish.com>

[Shell] regular expressions and wildcard characters

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.