『壹』 linuxsed命令詳解
sed 是一種在線編輯器,它一次處理一行內容。處理時,把當前處理的行存儲在臨時緩沖區中,稱為「模式空間」(pattern space),接著用sed命令處理緩沖區中的內容,處理完成後,把緩沖區的內容送往屏幕。接著處理下一行,這樣不斷重復,直到文件末尾。文件內容並沒有 改變,除非你使用重定向存儲輸出。Sed主要用來自動編輯一個或多個文件;簡化對文件的反復操作;編寫轉換程序等。
sed使用參數
復制代碼代碼如下:
[root@www ~]# sed [-nefr] [動作]
選項與參數:
-n :使用安靜(silent)模式。在一般 sed 的用法中,所有來自 STDIN 的數據一般都會被列出到終端上。但如果加上 -n 參數後,則只有經過sed 特殊處理的那一行(或者動作)才會被列出來。
-e :直接在命令列模式上進行 sed 的動作編輯;
-f :直接將 sed 的動作寫在一個文件內, -f filename 則可以運行 filename 內的 sed 動作;
-r :sed 的動作支持的是延伸型正規表示法的語法。(默認是基礎正規表示法語法)
-i :直接修改讀取的文件內容,而不是輸出到終端。
動作說明: [n1[,n2]]function
n1, n2 :不見得會存在,一般代表『選擇進行動作的行數』,舉例來說,如果我的動作是需要在 10 到 20 行之間進行的,則『 10,20[動作行為] 』
function:
a :新增, a 的後面可以接字串,而這些字串會在新的一行出現(目前的下一行)~
c :取代, c 的後面可以接字串,這些字串可以取代 n1,n2 之間的行!
d :刪除,因為是刪除啊,所以 d 後面通常不接任何咚咚;
i :插入, i 的後面可以接字串,而這些字串會在新的一行出現(目前的上一行);
p :列印,亦即將某個選擇的數據印出。通常 p 會與參數 sed -n 一起運行~
s :取代,可以直接進行取代的工作哩!通常這個 s 的動作可以搭配正規表示法!例如 1,20s/old/new/g 就是啦!
以行為單位的新增/刪除
將 /etc/passwd 的內容列出並且列印行號,同時,請將第 2~5 行刪除!
復制代碼代碼如下:
[root@www ~]# nl /etc/passwd | sed ƈ,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 的動作為 ƈ,5d' ,那個 d 就是刪除!因為 2-5 行給他刪除了,所以顯示的數據就沒有 2-5 行羅~ 另外,注意一下,原本應該是要下達 sed -e 才對,沒有 -e 也行啦!同時也要注意的是, sed 後面接的動作,請務必以 '' 兩個單引號括住喔!
只要刪除第 2 行
復制代碼代碼如下:
nl /etc/passwd | sed ƈd'
要刪除第 3 到最後一行
復制代碼代碼如下:
nl /etc/passwd | sed Ɖ,$d'
在第二行後(亦即是加在第三行)加上『drink tea?』字樣!
復制代碼代碼如下:
[root@www ~]# nl /etc/passwd | sed ƈa 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 ƈi drink tea'
如果是要增加兩行以上,在第二行後面加入兩行字,例如『Drink tea or .....』與『drink beer?』
復制代碼代碼如下:
[root@www ~]# nl /etc/passwd | sed ƈa 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
.....(後面省略).....
每一行之間都必須要以反斜杠『 』來進行新行的添加喔!所以,上面的例子中,我們可以發現在第一行的最後面就有 存在。
以行為單位的替換與顯示
將第2-5行的內容取代成為『No 2-5 number』呢?
復制代碼代碼如下:
[root@www ~]# nl /etc/passwd | sed ƈ,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
.....(後面省略).....
透過這個方法我們就能夠將數據整行取代了!
僅列出 /etc/passwd 文件內的第 5-7 行
復制代碼代碼如下:
[root@www ~]# nl /etc/passwd | sed -n Ƌ,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
可以透過這個 sed 的以行為單位的顯示功能, 就能夠將某一個文件內的某些行號選擇出來顯示。
數據的搜尋並顯示
搜索 /etc/passwd有root關鍵字的行
復制代碼代碼如下:
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找到,除了輸出所有行,還會輸出匹配行。
使用-n的時候將只列印包含模板的行。
復制代碼代碼如下:
nl /etc/passwd | sed -n '/root/p'
1 root:x:0:0:root:/root:/bin/bash
數據的搜尋並刪除
刪除/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已經刪除了
數據的搜尋並執行命令
找到匹配模式eastern的行後,
搜索/etc/passwd,找到root對應的行,執行後面花括弧中的一組命令,每個命令之間用分號分隔,這里把bash替換為blueshell,再輸出這行:
復制代碼代碼如下:
nl /etc/passwd | sed -n '/root/{s/bash/blueshell/;p}' 1 root:x:0:0:root:/root:/bin/blueshell
如果只替換/etc/passwd的第一個bash關鍵字為blueshell,就退出
復制代碼代碼如下:
nl /etc/passwd | sed -n '/bash/{s/bash/blueshell/;p;q}'
1 root:x:0:0:root:/root:/bin/blueshell
最後的q是退出。
數據的搜尋並替換
除了整行的處理模式之外, sed 還可以用行為單位進行部分數據的搜尋並取代。基本上 sed 的搜尋與替代的與 vi 相當的類似!他有點像這樣:
復制代碼代碼如下:
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
.....(以下省略).....
本機的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
接下來則是刪除後續的部分,亦即: 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
多點編輯
一條sed命令,刪除/etc/passwd第三行到末尾的數據,並把bash替換為blueshell
復制代碼代碼如下:
nl /etc/passwd | sed -e Ɖ,$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表示多點編輯,第一個編輯命令刪除/etc/passwd第三行到末尾的數據,第二條命令搜索bash替換為blueshell。
直接修改文件內容(危險動作)
sed 可以直接修改文件的內容,不必使用管道命令或數據流重導向! 不過,由於這個動作會直接修改到原始的文件,所以請你千萬不要隨便拿系統配置來測試! 我們還是使用下載的 regular_express.txt 文件來測試看看吧!
利用 sed 將 regular_express.txt 內每一行結尾若為 . 則換成 !
復制代碼代碼如下:
[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 的動作是新增,因此該文件最後新增『# This is a test』!
sed 的『 -i 』選項可以直接修改文件內容,這功能非常有幫助!舉例來說,如果你有一個 100 萬行的文件,你要在第 100 行加某些文字,此時使用 vim 可能會瘋掉!因為文件太大了!那怎辦?就利用 sed 啊!透過 sed 直接修改/取代的功能,你甚至不需要使用 vim 去修訂!
流編輯器sed:
sed一次處理一行文件並把輸出送往屏幕。sed把當前處理的行存儲在臨時緩沖區中,稱為模式空間(pattern space)。一旦sed完成對模式空間中的行的處理,模式空間中的行就被送往屏幕。行被處理完成之後,就被移出模式空間,程序接著讀入下一行,處理,顯示,移出......文件輸入的最後一行被處理完以後sed結束。通過存儲每一行在臨時緩沖區,然後在緩沖區中操作該行,保證了原始文件不會被破壞。
1. sed的命令和選項:
命令功能描述a在當前行的後面加入一行或者文本。c用新的文本改變或者替代本行的文本。d從pattern space位置刪除行。i在當前行的上面插入文本。h拷貝pattern space的內容到holding buffer(特殊緩沖區)。H追加pattern space的內容到holding buffer。g獲得holding buffer中的內容,並替代當前pattern space中的文本。G獲得holding buffer中的內容,並追加到當前pattern space的後面。n讀取下一個輸入行,用下一個命令處理新的行而不是用第一個命令。p列印pattern space中的行。P列印pattern space中的第一行。q退出sed。w file寫並追加pattern space到file的末尾。!表示後面的命令對所有沒有被選定的行發生作用。s/re/string用string替換
正則表達式re。=列印當前行號碼。替換標記g行內全面替換,如果沒有g,只替換第一個匹配。p列印行。x互換pattern space和holding buffer中的文本。y把一個字元翻譯為另一個字元(但是不能用於正則表達式)。選項-e允許多點編輯。-n取消默認輸出。
需要說明的是,sed中的正則和grep的基本相同,完全可以參照本系列的第一篇中的詳細說明。 2. sed實例:
/> 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選項取消了sed的默認行為。在沒有-n的時候,包含模板的行被列印兩次,但是在使用-n的時候將只列印包含模板的行。
/> 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 Ɖd' testfile #第三行被刪除,其他行默認輸出到屏幕。
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 Ɖ,$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表示命令作用於整個當前行。如果該行存在多個west,都將被替換為north,如果沒有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
#&符號表示替換字元串中被找到的部分。所有以兩個數字結束的行,最後的數字都將被它們自己替換,同時追加.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選項加p命令則表示只列印匹配行。
southeast SE Patricia Jones 4.0 .7 4 17
#模板Mar被包含在一對括弧中,並在特殊的寄存器中保存為tag 1,它將在後面作為1替換字元串,Margot被替換為Marlianne。
/> sed -n 's/(Mar)got/1lianne/p' testfile north NO Marlianne Weber 4.5 .89 5 9
#s後面的字元一定是分隔搜索字元串和替換字元串的分隔符,默認為斜杠,但是在s命令使用的情況下可以改變。不論什麼字元緊跟著s命令都認為是新的分隔符。這個技術在搜索含斜杠的模板時非常有用,例如搜索時間和路徑的時候。
/> 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所確定的范圍內的行都被列印,如果west出現在esst後面的行中,從west開始到下一個east,無論這個east出現在哪裡,二者之間的行都被列印,即使從west開始到文件的末尾還沒有出現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 Ƌ,/^northeast/p' testfile #列印從第五行開始到第一個以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選項表示多點編輯。第一個編輯命令是刪除第一到第三行。第二個編輯命令是用Jones替換Hemenway。
/> sed -e Ƈ,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的行後,執行後面花括弧中的一組命令,每個命令之間用逗號分隔,n表示定位到匹配行的下一行,s/AM/Archie/完成Archie到AM的替換,p和-n選項的合用,則只是列印作用到的行。
/> sed -n '/eastern/{n;s/AM/Archie/;p}' testfile northeast NE Archie Main Jr. 5.1 .94 3 13
#-e表示多點編輯,第一個編輯命令y將前三行中的所有小寫字母替換為大寫字母,-n表示不顯示替換後的輸出,第二個編輯命令將只是列印輸出轉換後的前三行。注意y不能用於正則。
/> sed -n -e Ƈ,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' -e Ƈ,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 ƈq' testfile #列印完第二行後退出。
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
#當模板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處理文件的時候,每一行都被保存在pattern space的臨時緩沖區中。除非行被刪除或者輸出被取消,否則所有被處理過的行都將列印在屏幕上。接著pattern space被清空,並存入新的一行等待處理。在下面的例子中,包含模板的northeast行被找到,並被放入pattern space中,h命令將其復制並存入一個稱為holding buffer的特殊緩沖區內。在第二個sed編輯命令中,當達到最後一行後,G命令告訴sed從holding buffer中取得該行,然後把它放回到pattern space中,且追加到現在已經存在於模式空間的行的末尾。
/> 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中復制到holding buffer中,d命令在將該行刪除,因此WE匹配行沒有在原來的位置被輸出。第二個命令搜索CT,一旦被找到,G命令將從holding buffer中取回行,並追加到當前pattern space的行末尾。簡單的說,WE所在的行被移動並追加到包含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
#第一個命令將匹配northeast的行從pattern space復制到holding buffer,第二個命令在讀取的文件的末尾時,g命令告訴sed從holding buffer中取得行,並把它放回到pattern space中,以替換已經存在於pattern space中的。簡單說就是包含模板northeast的行被復制並覆蓋了文件的末尾行。
/> 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命令復制到holding buffer,再被d命令刪除。結果可以看出WE的原有位置沒有輸出。第二個編輯命令將找到匹配CT的行,g命令將取得holding buffer中的行,並覆蓋當前pattern space中的行,即匹配CT的行。簡單的說,任何包含模板northeast的行都將被復制,並覆蓋包含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
#第一個編輯中的h命令將匹配Patricia的行復制到holding buffer中,第二個編輯中的x命令,會將holding buffer中的文本考慮到pattern space中,而pattern space中的文本被復制到holding buffer中。因此在列印匹配Margot行的地方列印了holding buffer中的文本,即第一個命令中匹配Patricia的行文本,第三個編輯命令會將交互後的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