sed一次處理一行文件並把輸出送往屏幕。sed把當前處理的行存儲在臨時緩衝區中,稱爲模式空間(pattern space)。一旦sed完成對模式空間中的行的處理,模式空間中的行就被送往屏幕。行被處理完成以後,就被移出模式空間,程序接着讀入下一行,處理,顯示,移出......文件輸入的最後一行被處理完之後sed結束。經過存儲每一行在臨時緩衝區,而後在緩衝區中操做該行,保證了原始文件不會被破壞。正則表達式
1. sed的命令和選項:sass
命令功能描述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 取消默認輸出。spa
須要說明的是,sed中的正則和grep的基本相同,徹底能夠參照本系列的第一篇中的詳細說明。翻譯
2. sed實例:3d
/> cat testfileci
northwest NW Charles Main 3.0 .98 3 34字符串
western WE Sharon Gray 5.3 .97 5 23string
southwest SW Lewis Dalsass 2.7 .8 2 18ast
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 '3d' 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 '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表示命令做用於整個當前行。若是該行存在多個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 '5,/^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 '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的行後,執行後面花括號中的一組命令,每一個命令之間用逗號分隔,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
點擊瞭解更多,瞭解更多哦