Regular expression of Linux shell script programming

Time:2021-5-3

A regular expression and wildcard

1 regular expression is used to match qualified strings in files. Regular expression includes matching. Grep, awk, SED and other commands can support regular expression

2. Wildcards are used to match qualified file names. Wildcards are exact matches. Ls, find and CP do not support regular expressions, so they can only be matched with shell’s own wildcards.

Two basic regular expressions

The test text of brother company is quoted here

这里写图片描述 

1 * the previous character is matched 0 or more times

grep “a*” test_rule.txt

Match all contents, including blank lines (because * can match 0 times)

这里写图片描述

grep “aa*” test_rule.txt

Match rows that contain at least one a

这里写图片描述

grep “aaa*” test_rule.txt

Match rows that contain at least two a’s

这里写图片描述

2. Match any character except newline character

grep “s..d” test_rule.txt

Matching s and D directly requires two character lines

这里写图片描述

grep “s.*d” test_rule.txt

Match s and D directly to any character

这里写图片描述

grep “.*” test_rule.txt

Match everything

这里写图片描述

3 ^ match start of line $match end of line

grep “^M” test_rule.txt

Match lines beginning with capital M

这里写图片描述

grep “n$” test_rule.txt

Match lines ending in lowercase n

这里写图片描述

grep -n “^$” test_rule.txt

Match blank lines

这里写图片描述

4 [] matches any character specified in brackets, only one character

grep “s[ao]id” test_rule.txt

Match the lines between S and I, either a or o

这里写图片描述

grep “[0-9]” test_rule.txt

Match any number

这里写图片描述

grep “^[a-z]” test_rule.txt

Matches lines that start with lowercase letters

这里写图片描述

5 [^] matches any character except in brackets

grep “^[^a-z]” test_rule.txt

Matches lines that do not start with lowercase letters

这里写图片描述

grep “^[^a-zA-Z]” test_rule.txt

Matches lines that do not start with a character

这里写图片描述

6 “\” escape character

grep “.$” test_rule.txt

Match lines ending with

这里写图片描述

7 {n} means that the character before it appears exactly n times

grep “a{3}” test_rule.txt

Match the line where the letter a appears three times in a row

这里写图片描述

grep “[0-9]{3}” test_rule.txt

Matches a row containing three consecutive numbers

这里写图片描述

8 {n,} means that the character before it appears no less than n times

grep “^[0-9]{3,}” test_rule.txt

Match lines that start with at least three consecutive numbers

这里写图片描述

9 {n, m} means that the character in front of it appears no less than n times, at most m times

grep “sa{1,3}i” test_rule.txt

Match at least one a and at most three a between S and I

这里写图片描述

Well, Linux shell programming – regular expression is summarized here. If you have any questions, please leave me a message and Xiaobian will reply you in time. Thank you very much for your support to developer!

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]