[root@master opt]# mkdir test[root@master opt]# cd test/[root@master test]# touch {1..20} {a..z}[root@master test]# ls1111315171920468 a c e g i k m o q s u w y
101214161823579 b d f h j l n p r t v x z
[root@master test]# ls | grep '^.$'123456789
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
[root@master test]# ls | grep '^..$'1011121314151617181920
[] //匹配指定范围内的任意单个字符
[root@master test]# ls | grep '^[1]$'1[root@master test]# ls | grep '^[1-5]$'12345[root@master test]# ls | grep '^[a]$'
a
[root@master test]# ls | grep '^[a-f]$'
a
b
c
d
e
f
[^] //匹配指定范围外的任意单个字符
[root@master test]# ls | grep '^[^a-z]$'123456789
匹配次数(贪婪模式)
* //匹配其前面的任意单个字符任意次
[root@master test]# touch abc abcc abccc abccc[root@master test]# ls | grep '^abc*$'
ab
abc
abcc
abccc
.* //任意长度的任意字符
[root@master test]# touch bc bcc bccc [root@master test]# ls | grep '^b.*$'
b
bc
bcc
bccc
\? //匹配其前面的任意单个字符1次或0次
[root@master test]# ls | grep '^b\?$'
b
[root@master test]# ls | grep '^bc\?$'
b
bc
\+ //匹配其前面的任意单个字符至少1次
[root@master test]# ls | grep '^bc\+$'bc
bcc
bccc
\{m,n\} //匹配其前面的任意单个字符至少m次,至多n次
[root@master test]# ls | grep '^abc\{1,\}$'
abc
abcc
abccc
[root@master test]# ls | grep '^abc\{1,2\}$'
abc
abcc
[root@master test]# ls | grep '^abc\{1,3\}$'
abc
abcc
abccc
位置锚定
^ //锚定行首,此字符后面的任意单个字符必须出现在行首
[root@master test]# ls | grep '^abc'
abc
abcc
abccc
$ //锚定行尾,此字符前面的任意单个字符必须出现在行尾
[root@master test]# ls | grep 'c$'
abc
abcc
abccc
bc
bcc
bccc
c
^$ //空白行
[root@master test]# vim a
hello world
hehe
[root@master test]# grep '^$' a
//此处是空行
[root@master test]# grep -v '^$' a //-v 表示取反
hello world //
hehe
[root@master test]# echo 'hello world yexiaotian'
hello world yexiaotian
[root@master test]# echo 'hello world yexiaotian' | sed 's/hello \(.*\) \(.*\)/hello \2 \1/'
hello yexiaotian world