全部博文(626)
分类: 其他UNIX
2013-09-29 14:28:46
UNIX Shell 编程(3)-UNIX Shell的正则表达式
匹配任何单个字符:句点(.)
比如:r.表示匹配r后跟任一个字符的模式。
匹配行首符号:^
比如:^George表示匹配以George开头的行
匹配行尾符号:$
比如:contents$表示匹配在行尾的字符串contents。
GNU Ed 0.8的用法
[root@localhost programs]# ed intro
253
/.../ #搜索前后都有空格的任意三个字符
The Unix operating system was pioneered by Ken
/ #重复上次搜索
Thompson and Dennis Ritchie at Bell Laboratories
/
in the late 1960s. One of the primary goals in
/
the design of the Unix system was to create an
/
environment that promoted efficient program
/
developments.
1,$p
The Unix operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
in the late 1960s. One of the primary goals in
the design of the Unix system was to create an
environment that promoted efficient program
developments.
1,$s/p.o/XXX/g #把所有“p.o”替换为“XXX”
1,$p #查看替换后的结果
The Unix operating system was XXXneered by Ken
ThomXXXn and Dennis Ritchie at Bell Laboratories
in the late 1960s. One of the primary goals in
the design of the Unix system was to create an
environment that XXXmoted efficient XXXgram
developments.
/^the/ #搜索以the开头的行
the design of the Unix system was to create an
1,$s/^/>>/ #在每行开头插入>>
1,$
>>developments.
1,$p #查看插入后结果
>>The Unix operating system was pioneered by Ken
>>Thompson and Dennis Ritchie at Bell Laboratories
>>in the late 1960s. One of the primary goals in
>>the design of the Unix system was to create an
>>environment that promoted efficient program
>>developments.
1,$s/^/ / #在每行开头插入空格
1,$p #查看结果
>>The Unix operating system was pioneered by Ken
>>Thompson and Dennis Ritchie at Bell Laboratories
>>in the late 1960s. One of the primary goals in
>>the design of the Unix system was to create an
>>environment that promoted efficient program
>>developments.
//.$/ #搜索以句点结束的行
developments.
1,$s/$/>>/ #在每行末尾添加>>
1,$p #查看结果
The Unix operating system was pioneered by Ken >>
Thompson and Dennis Ritchie at Bell Laboratories >>
in the late 1960s. One of the primary goals in >>
the design of the Unix system was to create an >>
environment that promoted efficient program >>
developments.>>
1,$s/..$// #删除每行最好两个字符
1,$p #查看结果
The Unix operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
in the late 1960s. One of the primary goals in
the design of the Unix system was to create an
environment that promoted efficient program
developments.
匹配字符组之一:[...]结构
/the/ 匹配含the的行
[tT]he 匹配the或The
如:
1,$s/[aeiouAEIOU]//g #删除所有元音字母
1,$p
Th nx prtng systm ws pnrd by Kn
Thmpsn nd Dnns Rtch t Bll Lbrtrs
n th lt 1960s. n f th prmry gls n
th dsgn f th nx systm ws t crt n
nvrnmnt tht prmtd ffcnt prgrm
dvlpmnts.
[0-9] 匹配数字
[a-z] 匹配小写字母
[a-zA-Z] 匹配大小写字母
如:
1,$s/[A-Z]/*/g #把所有大些字母替换为*
1,$p
*he *nix operating system was pioneered by *en
*hompson and *ennis *itchie at *ell *aboratories
in the late 1960s. *ne of the primary goals in
the design of the *nix system was to create an
environment that promoted efficient program
developments.
[^A-Z] 匹配除大些字母以外的任何字符
匹配零个到若干个字符:星号(*)
如下,把多个空格替换成一个空格:
[root@localhost programs]# ed lotsapaces
126
1,$p
This is an example of a
file that contains a lot
of blank spaces
1,$s/ */ /g
1,$p
This is an example of a
file that contains a lot
of blank spaces
再如:匹配一行中第一个e和最后一个e之间的所有内容,替换为+++:
[root@localhost programs]# ed lotsapaces
126
1,$s/e.*e/+++/g
1,$p
This is an +++ of a
file that contains a lot
of blank spaces
1,$s/[a-zA-Z][a-zA-Z]*/X/g
1,$p
X X X +++ X X
X X X X X
X X X
匹配精确数目的字符串:
XX* 指匹配至少一个连续的X
XXX* 指匹配至少两个连续的X
/{min,max/} 其中:min表示重复的最小次数,max表示最大次数。
保存匹配的字符串/(.../):
把字符包括在前加反斜杠的小括号中,可以捕获正则表达式匹配的字符串。
捕获的字符串存储在编号为1到9的寄存器中。
如下:
[root@localhost programs]# ed phonebook
155
1,$p
Alica Chebba 973-555-2015
Barbara Swingle 201-555-9257
Liz Stachiw 212-555-2298
Susan Goldberg 201-555-7776
Tony Iannino 973-555-1295
1,$s//(.*/) /(.*/)//2 /1/
1,$p
973-555-2015 Alica Chebba
201-555-9257 Barbara Swingle
212-555-2298 Liz Stachiw
201-555-7776 Susan Goldberg
973-555-1295 Tony Iannino
交换了前后字段。