UNIX Shell 編程(3)-UNIX Shell的Regex
匹配任何單個字元:句點(.)
比如: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表示最大次數。
儲存匹配的字串/(.../):
把字元包括在前加反斜線的小括弧中,可以捕獲Regex匹配的字串。
捕獲的字串儲存在編號為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
交換了前後欄位。