2017.10.12 shell sed命令詳解
http://www.cnblogs.com/mchina/archive/2012/06/30/2570523.html 感謝原作者
流編輯器sed
8.1 sed簡介
sed是stream editor的縮寫,一種流編輯器,它一次處理一行內容。處理時,把當前處理的行儲存在臨時緩衝區中,稱為“模式空間”(pattern space),接著用sed命令處理緩衝區中的內容,處理完成後,把緩衝區的內容送往螢幕。接著處理下一行,這樣不斷重複,直到檔案末尾。檔案內容並沒有改變,除非你使用重定向儲存輸出。sed主要用來自動編輯一個或多個檔案,簡化對檔案的反覆操作,編寫轉換程式等。
8.2 sed命令格式
sed [nefri] 'command' file(s)
常用選項:
-n 使用安靜(silent)模式。在一般sed的用法中,所有來自stdin的資料一般都會被列出到螢幕,但如果加上-n引數後,則只有經過sed特殊處理的那一行(或者command)才會被列出來。
-e 允許多點編輯。
-f 直接將sed的動作寫在一個檔案內,-f filename 則可以執行filename內的sed動作。
-r sed 的動作支援的是延伸型正規表示法的語法。(預設是基礎正規表示法語法)
-i 直接修改讀取的檔案內容,而不是由螢幕輸出。
常用command:
a\ 新增, a 的後面可以接字串,而這些字串會在新的一行出現(目前的下一行)~
c\ 取代, c 的後面可以接字串,這些字串可以取代 n1,n2 之間的行!
d 刪除,因為是刪除啊,所以 d 後面通常不接任何咚咚;
i\ 插入, i 的後面可以接字串,而這些字串會在新的一行出現(目前的上一行);
p 列印,亦即將某個選擇的資料印出。通常 p 會與引數 sed -n 一起運作~
s 取代,可以直接進行取代的工作哩!通常這個 s 的動作可以搭配正規表示法!例如 1,20s/old/new/g 就是啦!
高階command:
命令 | 功能描述 |
h | 拷貝pattern space的內容到holding buffer(特殊緩衝區)。 |
H | 追加pattern space的內容到holding buffer。 |
g | 獲得holding buffer中的內容,並替代當前pattern space中的文字。 |
G | 獲得holding buffer中的內容,並追加到當前pattern space的後面。 |
n | 讀取下一個輸入行,用下一個命令處理新的行而不是用第一個命令。 |
P | 列印pattern space中的第一行。 //大寫 |
q | 退出sed。 |
w file | 寫並追加pattern space到file的末尾。 |
! | 表示後面的命令對所有沒有被選定的行發生作用。 |
s/re/string | 用string替換正則表示式re。 |
= | 列印當前行號碼。 |
替換標記 | |
g | 行內全面替換,如果沒有g,只替換第一個匹配。 |
x | 互換pattern space和holding buffer中的文字。 |
y | 把一個字元翻譯為另一個字元(但是不能用於正則表示式)。 |
需要說明的是,sed中的正則和grep的基本相同,完全可以參照本系列的第一篇中的詳細說明。
8.3 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
例項1.1:如果模板north被找到,sed除了列印所有行之外,還有列印匹配行。
# sed '/north/p' testfile
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
例項1.2:-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
例項:列出第5-7行
# nl testfile |sed -n '5,7p'
5 southeast SE Patricia Hemenway 4.0 .7 4 17
6 eastern EA TB Savage 4.4 .84 5 20
7 northeast NE AM Main Jr. 5.1 .94 3 13
例項2.1:刪除第三行,其他行預設輸出到螢幕。
# nl testfile |sed '3d'
1 northwest NW Charles Main 3.0 .98 3 34
2 western WE Sharon Gray 5.3 .97 5 23
4 southern SO Suan Chin 5.1 .95 4 15
5 southeast SE Patricia Hemenway 4.0 .7 4 17
6 eastern EA TB Savage 4.4 .84 5 20
7 northeast NE AM Main Jr. 5.1 .94 3 13
8 north NO Margot Weber 4.5 .89 5 9
9 central CT Ann Stephens 5.7 .94 5 13
例項2.2:刪除2~5行
# nl testfile |sed '2,5d'
1 northwest NW Charles Main 3.0 .98 3 34
6 eastern EA TB Savage 4.4 .84 5 20
7 northeast NE AM Main Jr. 5.1 .94 3 13
8 north NO Margot Weber 4.5 .89 5 9
9 central CT Ann Stephens 5.7 .94 5 13
例項2.3:從第三行刪除到最後一行,其他行被列印。$表示最後一行。
# nl testfile |sed '3,$d'
1 northwest NW Charles Main 3.0 .98 3 34
2 western WE Sharon Gray 5.3 .97 5 23
例項2.4:刪除最後一行,其他行列印。
# nl testfile |sed '$d'
1 northwest NW Charles Main 3.0 .98 3 34
2 western WE Sharon Gray 5.3 .97 5 23
3 southwest SW Lewis Dalsass 2.7 .8 2 18
4 southern SO Suan Chin 5.1 .95 4 15
5 southeast SE Patricia Hemenway 4.0 .7 4 17
6 eastern EA TB Savage 4.4 .84 5 20
7 northeast NE AM Main Jr. 5.1 .94 3 13
8 north NO Margot Weber 4.5 .89 5 9
例項2.5:刪除所有包含north的行,其他行列印。
# nl testfile |sed '/north/d'
2 western WE Sharon Gray 5.3 .97 5 23
3 southwest SW Lewis Dalsass 2.7 .8 2 18
4 southern SO Suan Chin 5.1 .95 4 15
5 southeast SE Patricia Hemenway 4.0 .7 4 17
6 eastern EA TB Savage 4.4 .84 5 20
9 central CT Ann Stephens 5.7 .94 5 13
例項3.1:在第二行後(即加在第三行)加上"United States"。
# nl testfile |sed '2a United States'
1 northwest NW Charles Main 3.0 .98 3 34
2 western WE Sharon Gray 5.3 .97 5 23
United States
3 southwest SW Lewis Dalsass 2.7 .8 2 18
4 southern SO Suan Chin 5.1 .95 4 15
5 southeast SE Patricia Hemenway 4.0 .7 4 17
6 eastern EA TB Savage 4.4 .84 5 20
7 northeast NE AM Main Jr. 5.1 .94 3 13
8 north NO Margot Weber 4.5 .89 5 9
9 central CT Ann Stephens 5.7 .94 5 13
如果要在第二行前加,則命令為
# nl testfile |sed '2i United States'
例項3.2:在第二行後加上兩行文字。
# nl testfile |sed '2a United States \
> America'
1 northwest NW Charles Main 3.0 .98 3 34
2 western WE Sharon Gray 5.3 .97 5 23
United States
America
3 southwest SW Lewis Dalsass 2.7 .8 2 18
4 southern SO Suan Chin 5.1 .95 4 15
5 southeast SE Patricia Hemenway 4.0 .7 4 17
6 eastern EA TB Savage 4.4 .84 5 20
7 northeast NE AM Main Jr. 5.1 .94 3 13
8 north NO Margot Weber 4.5 .89 5 9
9 central CT Ann Stephens 5.7 .94 5 13
若要新增兩行,則每一行之間都必須要以反斜線“\”來進行新行的增加。
例項4.1:將第2~5行的內容取代為“No 2-5 number”。
# nl testfile |sed '2,5c No 2-5 number'
1 northwest NW Charles Main 3.0 .98 3 34
No 2-5 number
6 eastern EA TB Savage 4.4 .84 5 20
7 northeast NE AM Main Jr. 5.1 .94 3 13
8 north NO Margot Weber 4.5 .89 5 9
9 central CT Ann Stephens 5.7 .94 5 13
例項3.1:s表示替換,g表示命令作用於整個當前行。如果該行存在多個west,都將被替換為north,如果沒有g,則只是替換第一個匹配。
格式:sed 's/要替換的字串/新的字串/g'
# nl testfile |sed 's/west/north/g'
1 northnorth NW Charles Main 3.0 .98 3 34
2 northern WE Sharon Gray 5.3 .97 5 23
3 southnorth SW Lewis Dalsass 2.7 .8 2 18
4 southern SO Suan Chin 5.1 .95 4 15
5 southeast SE Patricia Hemenway 4.0 .7 4 17
6 eastern EA TB Savage 4.4 .84 5 20
7 northeast NE AM Main Jr. 5.1 .94 3 13
8 north NO Margot Weber 4.5 .89 5 9
9 central CT Ann Stephens 5.7 .94 5 13
例項3.2:-n表示只打印匹配行,如果某一行的開頭是west,則替換為north。
# sed -n 's/^west/north/p' testfile
northern WE Sharon Gray 5.3 .97 5 23
例項3.3:&符號表示替換字串中被找到的部分。所有以兩個數字結束的行,最後的數字都將被它們自己替換,同時追加.5。
# nl testfile |sed 's/[0-9][0-9]$/&.5/'
1 northwest NW Charles Main 3.0 .98 3 34.5
2 western WE Sharon Gray 5.3 .97 5 23.5
3 southwest SW Lewis Dalsass 2.7 .8 2 18.5
4 southern SO Suan Chin 5.1 .95 4 15.5
5 southeast SE Patricia Hemenway 4.0 .7 4 17.5
6 eastern EA TB Savage 4.4 .84 5 20.5
7 northeast NE AM Main Jr. 5.1 .94 3 13.5
8 north NO Margot Weber 4.5 .89 5 9
9 central CT Ann Stephens 5.7 .94 5 13.5
例項3.4:所有的Hemenway被替換為Jones。-n選項加p命令則表示只打印匹配行。
# nl testfile |sed -n 's/Hemenway/Jones/gp'
5 southeast SE Patricia Jones 4.0 .7 4 17
例項3.5:模板Mar被包含在一對括號中,並在特殊的暫存器中儲存為tag 1,它將在後面作為\1替換字串,Margot被替換為Marlianne。
# nl testfile |sed -n 's/\(Mar\)got/\1lianne/p'
8 north NO Marlianne Weber 4.5 .89 5 9
例項3.6: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
# sed '[email protected]@[email protected]' 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
例項3.7:所有在模板west和east所確定的範圍內的行都被列印,如果west出現在east後面的行中,從west開始到下一個east,無論這個 east出現在哪裡,二者之間的行都被列印,即使從west開始到檔案的末尾還沒有出現east,那麼從west到末尾的所有行都將列印。
# nl testfile |sed -n '/south/,/east/p'
3 southwest SW Lewis Dalsass 2.7 .8 2 18
4 southern SO Suan Chin 5.1 .95 4 15
5 southeast SE Patricia Hemenway 4.0 .7 4 17
例項3.8:列印從第五行開始到第一個以northeast開頭的行之間的所有行。
# sed -n '5,/^northeast/p' testfile
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
例項3.9:-e選項表示多點編輯。第一個編輯命令是刪除第一到第三行。第二個編輯命令是用Jones替換Hemenway。
# nl testfile |sed -e '1,3d' -e 's/Hemenway/Jones/'
4 southern SO Suan Chin 5.1 .95 4 15
5 southeast SE Patricia Jones 4.0 .7 4 17
6 eastern EA TB Savage 4.4 .84 5 20
7 northeast NE AM Main Jr. 5.1 .94 3 13
8 north NO Margot Weber 4.5 .89 5 9
9 central CT Ann Stephens 5.7 .94 5 13
例項3.10:將所有匹配含有north的行寫入newfile中。
# sed -n '/north/w newfile' testfile
# 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
例項4.1:i是插入命令,在匹配模式行前插入文字。
# sed '/eastern/i\NEW ENGLAND REGION' 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
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
例項5.1:找到匹配模式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 '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
例項:當模板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
north NO Margot Weber 4.5 .89 5 9