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的基本相同:
正則表達式基本語法描述:Linux Shell環境下提供了兩種正則表達式規則,一個是基本正則表達式(BRE),另一個是擴展正則表達式(ERE)。
下面是這兩種表達式的語法列表,需要注意的是,如果沒有明確指出的Meta字符,其將可同時用於BRE和ERE,否則將盡適用於指定的模式。
正則元字符 | 模式含義 | 用例 |
\ | 通常用於關閉其後續字符的特殊意義,恢復其原意。 | \(...\),這裏的括號僅僅表示括號。 |
. | 匹配任何單個字符。 | a.b,將匹配abb、acb等 |
* | 匹配它之前的0-n個的單個字符。 | a*b,將匹配ab、aab、aaab等。 |
^ | 匹配緊接着的正則表達式,在行的起始處。 | ^ab,將匹配abc、abd等,但是不匹配cab。 |
$ | 匹配緊接着的正則表達式,在行的結尾處。 | ab$,將匹配ab、cab等,但是不匹配abc。 |
[...] | 方括號表達式,匹配其內部任何字符。其中-表示連續字符的範圍,^符號置於方括號裏第一個字符則有反向的含義,即匹配不在列表內(方括號)的任何字符。如果想讓]和-表示其原意,需要將其放置在方括號的首字符位置,如[]ab]或[-ab],如這兩個字符同時存在,則將]放置在首字符位置,-放置在最尾部,如[]ab-]。 | [a-bA-Z0-9!]表示所有的大小寫字母,數字和感嘆號。[^abc]表示a、b、c之外的所有字符。[Tt]om,可以匹配Tom和tom。 |
\{n,m\} | 區間表達式,匹配在它前面的單個字符重複出現的次數區間,\{n\}表示重複n次;\{n,\}表示至少重複n次;\{n,m\}表示重複n到m次。 | ab\{2\}表示abb;ab\{2,\}表示abb、abbb等。ab\{2,4\}表示abb、abbb和abbbb。 |
\(...\) | 將圓括號之間的模式存儲在特殊“保留空間”。最多可以將9個獨立的子模式存儲在單個模式中。匹配於子模式的文本,可以通過轉義序列\1到\9,被重複使用在相同模式裏。 | \(ab\).*\1表示ab組合出現兩次,兩次之間可存在任何數目的任何字符,如abcdab、abab等。 |
{n,m}(ERE) | 其功能等同於上面的\{n,m\},只是不再寫\轉義符了。 | ab+匹配ab、abbb等,但是不匹配a。 |
+(ERE) | 和前面的星號相比,+匹配的是前面正則表達式的1-n個實例。 | |
?(ERE) | 匹配前面正則表達式的0個或1個。 | ab?僅匹配a或ab。 |
|(ERE) | 匹配於|符號前後的正則表達式。 | (ab|cd)匹配ab或cd。 |
[:alpha:] | 匹配字母字符。 | [[:alpha:]!]ab$匹配cab、dab和!ab。 |
[:alnum:] | 匹配字母和數字字符。 | [[:alnum:]]ab$匹配1ab、aab。 |
[:blank:] | 匹配空格(space)和Tab字符。 | [[:alnum:]]ab$匹配1ab、aab。 |
[:cntrl:] | 匹配控制字符。 | |
[:digit:] | 匹配數字字符。 | |
[:graph:] | 匹配非空格字符。 | |
[:lower:] | 匹配小寫字母字符。 | |
[:upper:] | 匹配大寫字母字符。 | |
[:punct:] | 匹配標點字符。 | |
[:space:] | 匹配空白(whitespace)字符。 | |
[:xdigit:] | 匹配十六進制數字。 | |
\w | 匹配任何字母和數字組成的字符,等同於[[:alnum:]_] | |
\W | 匹配任何非字母和數字組成的字符,等同於[^[:alnum:]_] | |
\<\> | 匹配單詞的起始和結尾。 | \<read匹配readme,me\>匹配readme。 |
下面的列表給出了Linux Shell中常用的工具或命令分別支持的正則表達式的類型。
grep | sed | vi | egrep | awk | |
BRE | * | * | * | ||
ERE | * | * |
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 '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