sed 是一種在線編輯器,它一次處理一行內(nèi)容。處理時(shí),把當(dāng)前處理的行存儲(chǔ)在臨時(shí)緩沖區(qū)中,稱為“模式空間”(pattern space),接著用sed命令處理緩沖區(qū)中的內(nèi)容,處理完成后,把緩沖區(qū)的內(nèi)容送往屏幕。接著處理下一行,這樣不斷重復(fù),直到文件末尾。文件內(nèi)容并沒有 改變,除非你使用重定向存儲(chǔ)輸出。Sed主要用來自動(dòng)編輯一個(gè)或多個(gè)文件;簡(jiǎn)化對(duì)文件的反復(fù)操作;編寫轉(zhuǎn)換程序等。
sed使用參數(shù)
[root@www ~]# sed [-nefr] [動(dòng)作]
選項(xiàng)與參數(shù):
-n :使用安靜(silent)模式。在一般 sed 的用法中,所有來自 STDIN 的數(shù)據(jù)一般都會(huì)被列出到終端上。但如果加上 -n 參數(shù)后,則只有經(jīng)過sed 特殊處理的那一行(或者動(dòng)作)才會(huì)被列出來。
-e :直接在命令列模式上進(jìn)行 sed 的動(dòng)作編輯;
-f :直接將 sed 的動(dòng)作寫在一個(gè)文件內(nèi), -f filename 則可以運(yùn)行 filename 內(nèi)的 sed 動(dòng)作;
-r :sed 的動(dòng)作支持的是延伸型正規(guī)表示法的語法。(默認(rèn)是基礎(chǔ)正規(guī)表示法語法)
-i :直接修改讀取的文件內(nèi)容,而不是輸出到終端。/p>
p>動(dòng)作說明: [n1[,n2]]function
n1, n2 :不見得會(huì)存在,一般代表『選擇進(jìn)行動(dòng)作的行數(shù)』,舉例來說,如果我的動(dòng)作是需要在 10 到 20 行之間進(jìn)行的,則『 10,20[動(dòng)作行為] 』/p>
p>function:
a :新增, a 的后面可以接字串,而這些字串會(huì)在新的一行出現(xiàn)(目前的下一行)~
c :取代, c 的后面可以接字串,這些字串可以取代 n1,n2 之間的行!
d :刪除,因?yàn)槭莿h除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而這些字串會(huì)在新的一行出現(xiàn)(目前的上一行);
p :列印,亦即將某個(gè)選擇的數(shù)據(jù)印出。通常 p 會(huì)與參數(shù) sed -n 一起運(yùn)行~
s :取代,可以直接進(jìn)行取代的工作哩!通常這個(gè) s 的動(dòng)作可以搭配正規(guī)表示法!例如 1,20s/old/new/g 就是啦!
以行為單位的新增/刪除
將 /etc/passwd 的內(nèi)容列出并且列印行號(hào),同時(shí),請(qǐng)將第 2~5 行刪除!
[root@www ~]# nl /etc/passwd | sed '2,5d'
1 root:x:0:0:root:/root:/bin/bash
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
.....(后面省略).....
sed 的動(dòng)作為 '2,5d' ,那個(gè) d 就是刪除!因?yàn)?2-5 行給他刪除了,所以顯示的數(shù)據(jù)就沒有 2-5 行羅~ 另外,注意一下,原本應(yīng)該是要下達(dá) sed -e 才對(duì),沒有 -e 也行啦!同時(shí)也要注意的是, sed 后面接的動(dòng)作,請(qǐng)務(wù)必以 '' 兩個(gè)單引號(hào)括住喔!
只要?jiǎng)h除第 2 行
nl /etc/passwd | sed '2d'
要?jiǎng)h除第 3 到最后一行
nl /etc/passwd | sed '3,$d'
在第二行后(亦即是加在第三行)加上『drink tea?』字樣!
[root@www ~]# nl /etc/passwd | sed '2a drink tea'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
drink tea
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
.....(后面省略).....
那如果是要在第二行前
nl /etc/passwd | sed '2i drink tea'
如果是要增加兩行以上,在第二行后面加入兩行字,例如『Drink tea or .....』與『drink beer?』
[root@www ~]# nl /etc/passwd | sed '2a Drink tea or ......\
> drink beer ?'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
Drink tea or ......
drink beer ?
daemon:x:2:2:daemon:/sbin:/sbin/nologin
.....(后面省略).....
每一行之間都必須要以反斜杠『 \ 』來進(jìn)行新行的添加喔!所以,上面的例子中,我們可以發(fā)現(xiàn)在第一行的最后面就有 \ 存在。
以行為單位的替換與顯示
將第2-5行的內(nèi)容取代成為『No 2-5 number』呢?
[root@www ~]# nl /etc/passwd | sed '2,5c No 2-5 number'
1 root:x:0:0:root:/root:/bin/bash
No 2-5 number
6 sync:x:5:0:sync:/sbin:/bin/sync
.....(后面省略).....
透過這個(gè)方法我們就能夠?qū)?shù)據(jù)整行取代了!
僅列出 /etc/passwd 文件內(nèi)的第 5-7 行
[root@www ~]# nl /etc/passwd | sed -n '5,7p'
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
可以透過這個(gè) sed 的以行為單位的顯示功能, 就能夠?qū)⒛骋粋€(gè)文件內(nèi)的某些行號(hào)選擇出來顯示。
數(shù)據(jù)的搜尋并顯示
搜索 /etc/passwd有root關(guān)鍵字的行
nl /etc/passwd | sed '/root/p'
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
....下面忽略
如果root找到,除了輸出所有行,還會(huì)輸出匹配行。
使用-n的時(shí)候?qū)⒅淮蛴“0宓男小?/p>
nl /etc/passwd | sed -n '/root/p'
1 root:x:0:0:root:/root:/bin/bash
數(shù)據(jù)的搜尋并刪除
刪除/etc/passwd所有包含root的行,其他行輸出
nl /etc/passwd | sed '/root/d'
2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh
3 bin:x:2:2:bin:/bin:/bin/sh
....下面忽略
#第一行的匹配root已經(jīng)刪除了
數(shù)據(jù)的搜尋并執(zhí)行命令
找到匹配模式eastern的行后,
搜索/etc/passwd,找到root對(duì)應(yīng)的行,執(zhí)行后面花括號(hào)中的一組命令,每個(gè)命令之間用分號(hào)分隔,這里把bash替換為blueshell,再輸出這行:
nl /etc/passwd | sed -n '/root/{s/bash/blueshell/;p}' 1 root:x:0:0:root:/root:/bin/blueshell
如果只替換/etc/passwd的第一個(gè)bash關(guān)鍵字為blueshell,就退出
nl /etc/passwd | sed -n '/bash/{s/bash/blueshell/;p;q}'
1 root:x:0:0:root:/root:/bin/blueshell
最后的q是退出。
數(shù)據(jù)的搜尋并替換
除了整行的處理模式之外, sed 還可以用行為單位進(jìn)行部分?jǐn)?shù)據(jù)的搜尋并取代。基本上 sed 的搜尋與替代的與 vi 相當(dāng)?shù)念愃疲∷悬c(diǎn)像這樣:
sed 's/要被取代的字串/新的字串/g'
先觀察原始信息,利用 /sbin/ifconfig 查詢 IP
[root@www ~]# /sbin/ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:90:CC:A6:34:84
inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::290:ccff:fea6:3484/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
.....(以下省略).....
本機(jī)的ip是192.168.1.100。
將 IP 前面的部分予以刪除
[root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g'
192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
接下來則是刪除后續(xù)的部分,亦即: 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
將 IP 后面的部分予以刪除
[root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
192.168.1.100
多點(diǎn)編輯
一條sed命令,刪除/etc/passwd第三行到末尾的數(shù)據(jù),并把bash替換為blueshell
nl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell/'
1 root:x:0:0:root:/root:/bin/blueshell
2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh
-e表示多點(diǎn)編輯,第一個(gè)編輯命令刪除/etc/passwd第三行到末尾的數(shù)據(jù),第二條命令搜索bash替換為blueshell。
直接修改文件內(nèi)容(危險(xiǎn)動(dòng)作)
sed 可以直接修改文件的內(nèi)容,不必使用管道命令或數(shù)據(jù)流重導(dǎo)向! 不過,由於這個(gè)動(dòng)作會(huì)直接修改到原始的文件,所以請(qǐng)你千萬不要隨便拿系統(tǒng)配置來測(cè)試! 我們還是使用下載的 regular_express.txt 文件來測(cè)試看看吧!
利用 sed 將 regular_express.txt 內(nèi)每一行結(jié)尾若為 . 則換成 !
[root@www ~]# sed -i 's/\.$/\!/g' regular_express.txt
利用 sed 直接在 regular_express.txt 最后一行加入『# This is a test』
[root@www ~]# sed -i '$a # This is a test' regular_express.txt
由於 $ 代表的是最后一行,而 a 的動(dòng)作是新增,因此該文件最后新增『# This is a test』!
sed 的『 -i 』選項(xiàng)可以直接修改文件內(nèi)容,這功能非常有幫助!舉例來說,如果你有一個(gè) 100 萬行的文件,你要在第 100 行加某些文字,此時(shí)使用 vim 可能會(huì)瘋掉!因?yàn)槲募罅?!那怎辦?就利用 sed 啊!透過 sed 直接修改/取代的功能,你甚至不需要使用 vim 去修訂!
流編輯器sed:
sed一次處理一行文件并把輸出送往屏幕。sed把當(dāng)前處理的行存儲(chǔ)在臨時(shí)緩沖區(qū)中,稱為模式空間(pattern space)。一旦sed完成對(duì)模式空間中的行的處理,模式空間中的行就被送往屏幕。行被處理完成之后,就被移出模式空間,程序接著讀入下一行,處理,顯示,移出......文件輸入的最后一行被處理完以后sed結(jié)束。通過存儲(chǔ)每一行在臨時(shí)緩沖區(qū),然后在緩沖區(qū)中操作該行,保證了原始文件不會(huì)被破壞。
1. sed的命令和選項(xiàng):
命令 |
功能描述 |
a\ |
在當(dāng)前行的后面加入一行或者文本。 |
c\ |
用新的文本改變或者替代本行的文本。 |
d |
從pattern space位置刪除行。 |
i\ |
在當(dāng)前行的上面插入文本。 |
h |
拷貝pattern space的內(nèi)容到holding buffer(特殊緩沖區(qū))。 |
H |
追加pattern space的內(nèi)容到holding buffer。 |
g |
獲得holding buffer中的內(nèi)容,并替代當(dāng)前pattern space中的文本。 |
G |
獲得holding buffer中的內(nèi)容,并追加到當(dāng)前pattern space的后面。 |
n |
讀取下一個(gè)輸入行,用下一個(gè)命令處理新的行而不是用第一個(gè)命令。 |
p |
打印pattern space中的行。 |
P |
打印pattern space中的第一行。 |
q |
退出sed。 |
w file |
寫并追加pattern space到file的末尾。 |
! |
表示后面的命令對(duì)所有沒有被選定的行發(fā)生作用。 |
s/re/string |
用string替換正則表達(dá)式re。 |
= |
打印當(dāng)前行號(hào)碼。 |
替換標(biāo)記 |
|
g |
行內(nèi)全面替換,如果沒有g(shù),只替換第一個(gè)匹配。 |
p |
打印行。 |
x |
互換pattern space和holding buffer中的文本。 |
y |
把一個(gè)字符翻譯為另一個(gè)字符(但是不能用于正則表達(dá)式)。 |
選項(xiàng) |
|
-e |
允許多點(diǎn)編輯。 |
-n |
取消默認(rèn)輸出。 |
需要說明的是,sed中的正則和grep的基本相同,完全可以參照本系列的第一篇中的詳細(xì)說明。
2. sed實(shí)例:
/> cat testfile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
/> sed '/north/p' testfile #如果模板north被找到,sed除了打印所有行之外,還有打印匹配行。
northwest NW Charles Main 3.0 .98 3 34
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
#-n選項(xiàng)取消了sed的默認(rèn)行為。在沒有-n的時(shí)候,包含模板的行被打印兩次,但是在使用-n的時(shí)候?qū)⒅淮蛴“0宓男小?br /> /> sed -n '/north/p' testfile
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
/> sed '3d' testfile #第三行被刪除,其他行默認(rèn)輸出到屏幕。
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
/> sed '3,$d' testfile #從第三行刪除到最后一行,其他行被打印。$表示最后一行。
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
/> sed '$d' testfile #刪除最后一行,其他行打印。
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
/> sed '/north/d' testfile #刪除所有包含north的行,其他行打印。
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
central CT Ann Stephens 5.7 .94 5 13
#s表示替換,g表示命令作用于整個(gè)當(dāng)前行。如果該行存在多個(gè)west,都將被替換為north,如果沒有g(shù),則只是替換第一個(gè)匹配。
/> sed 's/west/north/g' testfile
northnorth NW Charles Main 3.0 .98 3 34
northern WE Sharon Gray 5.3 .97 5 23
southnorth SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
/> sed -n 's/^west/north/p' testfile #-n表示只打印匹配行,如果某一行的開頭是west,則替換為north。
northern WE Sharon Gray 5.3 .97 5 23
#符號(hào)表示替換字符串中被找到的部分。所有以兩個(gè)數(shù)字結(jié)束的行,最后的數(shù)字都將被它們自己替換,同時(shí)追加.5。
/> sed 's/[0-9][0-9]$/.5/' testfile
northwest NW Charles Main 3.0 .98 3 34.5
western WE Sharon Gray 5.3 .97 5 23.5
southwest SW Lewis Dalsass 2.7 .8 2 18.5
southern SO Suan Chin 5.1 .95 4 15.5
southeast SE Patricia Hemenway 4.0 .7 4 17.5
eastern EA TB Savage 4.4 .84 5 20.5
northeast NE AM Main Jr. 5.1 .94 3 13.5
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13.5
/> sed -n 's/Hemenway/Jones/gp' testfile #所有的Hemenway被替換為Jones。-n選項(xiàng)加p命令則表示只打印匹配行。
southeast SE Patricia Jones 4.0 .7 4 17
#模板Mar被包含在一對(duì)括號(hào)中,并在特殊的寄存器中保存為tag 1,它將在后面作為\1替換字符串,Margot被替換為Marlianne。
/> sed -n 's/\(Mar\)got/\1lianne/p' testfile
north NO Marlianne Weber 4.5 .89 5 9
#s后面的字符一定是分隔搜索字符串和替換字符串的分隔符,默認(rèn)為斜杠,但是在s命令使用的情況下可以改變。不論什么字符緊跟著s命令都認(rèn)為是新的分隔符。這個(gè)技術(shù)在搜索含斜杠的模板時(shí)非常有用,例如搜索時(shí)間和路徑的時(shí)候。
/> sed 's#3#88#g' testfile
northwest NW Charles Main 88.0 .98 88 884
western WE Sharon Gray 5.88 .97 5 288
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 88 188
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 188
#所有在模板west和east所確定的范圍內(nèi)的行都被打印,如果west出現(xiàn)在esst后面的行中,從west開始到下一個(gè)east,無論這個(gè)east出現(xiàn)在哪里,二者之間的行都被打印,即使從west開始到文件的末尾還沒有出現(xiàn)east,那么從west到末尾的所有行都將打印。
/> sed -n '/west/,/east/p' testfile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
/> sed -n '5,/^northeast/p' testfile #打印從第五行開始到第一個(gè)以northeast開頭的行之間的所有行。
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
#-e選項(xiàng)表示多點(diǎn)編輯。第一個(gè)編輯命令是刪除第一到第三行。第二個(gè)編輯命令是用Jones替換Hemenway。
/> sed -e '1,3d' -e 's/Hemenway/Jones/' testfile
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Jones 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
/> sed -n '/north/w newfile' testfile #將所有匹配含有north的行寫入newfile中。
/> cat newfile
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
/> sed '/eastern/i\ NEW ENGLAND REGION' testfile #i是插入命令,在匹配模式行前插入文本。
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
NEW ENGLAND REGION
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
#找到匹配模式eastern的行后,執(zhí)行后面花括號(hào)中的一組命令,每個(gè)命令之間用逗號(hào)分隔,n表示定位到匹配行的下一行,s/AM/Archie/完成Archie到AM的替換,p和-n選項(xiàng)的合用,則只是打印作用到的行。
/> sed -n '/eastern/{n;s/AM/Archie/;p}' testfile
northeast NE Archie Main Jr. 5.1 .94 3 13
#-e表示多點(diǎn)編輯,第一個(gè)編輯命令y將前三行中的所有小寫字母替換為大寫字母,-n表示不顯示替換后的輸出,第二個(gè)編輯命令將只是打印輸出轉(zhuǎn)換后的前三行。注意y不能用于正則。
/> sed -n -e '1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' -e '1,3p' testfile
NORTHWEST NW CHARLES MAIN 3.0 .98 3 34
WESTERN WE SHARON GRAY 5.3 .97 5 23
SOUTHWEST SW LEWIS DALSASS 2.7 .8 2 18
/> sed '2q' testfile #打印完第二行后退出。
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
#當(dāng)模板Lewis在某一行被匹配,替換命令首先將Lewis替換為Joseph,然后再用q退出sed。
/> sed '/Lewis/{s/Lewis/Joseph/;q;}' testfile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Joseph Dalsass 2.7 .8 2 18
#在sed處理文件的時(shí)候,每一行都被保存在pattern space的臨時(shí)緩沖區(qū)中。除非行被刪除或者輸出被取消,否則所有被處理過的行都將打印在屏幕上。接著pattern space被清空,并存入新的一行等待處理。在下面的例子中,包含模板的northeast行被找到,并被放入pattern space中,h命令將其復(fù)制并存入一個(gè)稱為holding buffer的特殊緩沖區(qū)內(nèi)。在第二個(gè)sed編輯命令中,當(dāng)達(dá)到最后一行后,G命令告訴sed從holding buffer中取得該行,然后把它放回到pattern space中,且追加到現(xiàn)在已經(jīng)存在于模式空間的行的末尾。
/> sed -e '/northeast/h' -e '$G' testfile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
northeast NE AM Main Jr. 5.1 .94 3 13
#如果模板WE在某一行被匹配,h命令將使得該行從pattern space中復(fù)制到holding buffer中,d命令在將該行刪除,因此WE匹配行沒有在原來的位置被輸出。第二個(gè)命令搜索CT,一旦被找到,G命令將從holding buffer中取回行,并追加到當(dāng)前pattern space的行末尾。簡(jiǎn)單的說,WE所在的行被移動(dòng)并追加到包含CT行的后面。
/> sed -e '/WE/{h;d;}' -e '/CT/{G;}' testfile
northwest NW Charles Main 3.0 .98 3 34
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
western WE Sharon Gray 5.3 .97 5 23
#第一個(gè)命令將匹配northeast的行從pattern space復(fù)制到holding buffer,第二個(gè)命令在讀取的文件的末尾時(shí),g命令告訴sed從holding buffer中取得行,并把它放回到pattern space中,以替換已經(jīng)存在于pattern space中的。簡(jiǎn)單說就是包含模板northeast的行被復(fù)制并覆蓋了文件的末尾行。
/> sed -e '/northeast/h' -e '$g' testfile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
northeast NE AM Main Jr. 5.1 .94 3 13
#模板WE匹配的行被h命令復(fù)制到holding buffer,再被d命令刪除。結(jié)果可以看出WE的原有位置沒有輸出。第二個(gè)編輯命令將找到匹配CT的行,g命令將取得holding buffer中的行,并覆蓋當(dāng)前pattern space中的行,即匹配CT的行。簡(jiǎn)單的說,任何包含模板northeast的行都將被復(fù)制,并覆蓋包含CT的行。
/> sed -e '/WE/{h;d;}' -e '/CT/{g;}' testfile
northwest NW Charles Main 3.0 .98 3 34
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
western WE Sharon Gray 5.3 .97 5 23
#第一個(gè)編輯中的h命令將匹配Patricia的行復(fù)制到holding buffer中,第二個(gè)編輯中的x命令,會(huì)將holding buffer中的文本考慮到pattern space中,而pattern space中的文本被復(fù)制到holding buffer中。因此在打印匹配Margot行的地方打印了holding buffer中的文本,即第一個(gè)命令中匹配Patricia的行文本,第三個(gè)編輯命令會(huì)將交互后的holding buffer中的文本在最后一行的后面打印出來。
/> sed -e '/Patricia/h' -e '/Margot/x' -e '$G' testfile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
southeast SE Patricia Hemenway 4.0 .7 4 17
central CT Ann Stephens 5.7 .94 5 13