正则表达式

扫码查看
\反斜线,转义特殊字符
$ cat data2
The cost is $4.00
$ sed -n '/\$/p' data2
The cost is $4.00
锚字符
^匹配行首
$ cat data3
This is a test line.
this is another test line.
A line that tests this feature.
Yet more testing of this
$ sed -n '/^this/p' data3
this is another test line.
$匹配行尾
[root@localhost ~]# sed -n '/this$/p' data3.txt
Yet more testing of this
.点号用来匹配除换行符以外的任意单个字符。
[root@localhost ~]# cat data6.sh
The cat is sleeping.
That is a very nice hat.
This test is at line four.
at ten o'clock we'll go home.
匹配任何at的文本
[root@localhost ~]# sed -n '/.at/p' data6.sh
The cat is sleeping.
That is a very nice hat.
This test is at line four.
[]匹配字符组,
[root@localhost ~]# sed -n '/[ch]at/p' data6.sh
The cat is sleeping.
That is a very nice hat.
chat 
可以在单个表达式中用多个字符组。
$ echo "Yes" | sed -n '/[Yy][Ee][Ss]/p'
Yes
$ echo "yEs" | sed -n '/[Yy][Ee][Ss]/p'
yEs
$ echo "yeS" | sed -n '/[Yy][Ee][Ss]/p'
yeS
$

匹配数字
$ cat data7
This line doesn't contain a number. 
This line has 1 number on it.
This line a number 2 on it.
This line has a number 4 on it.
$ sed -n '/[0123]/p' data7
This line has 1 number on it.
This line a number 2 on it.

$ cat data8
60633
46201
223001
4353
22203
$ sed -n '
>/[0123456789][0123456789][0123456789][0123456789][0123456789]/p
>' data8 
60633
46201
223001
22203
成功过滤掉了不可能是邮编的那些过短的数字,因为最后一个字符组
没有字符可匹配。但它也通过了那个六位数,尽管我们只定义了5个字符组

如果要确保只匹配五位数,就必须将匹配的字符和其他字符分开,要么用空格,要么指明行首和行尾
$ sed -n '
> /^
[0123456789][0123456789][0123456789][0123456789][0123456789]$/p
> ' data8 
60633
46201
22203
排除型字符组,匹配组中没有的字符
$ sed -n '/[^
ch]at/p' data6 
This test is at line four.
12-16 18:08
查看更多