一、什么是sed
sed
是一個很好的文件處理工具,本身是一個管道命令,主要是以行為單位進行處理,可以將數據行進行替換、刪除、新增、選取等特定工作。
二、sed命令格式和選項
·2.1 語法形式
命令 | 功能描述 |
---|---|
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實例
# 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'
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
實例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'
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
實例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
參考文獻:
http://www.jb51.net/LINUXjishu/144593.html
http://www.cnblogs.com/mchina/archive/2012/06/30/2570523.html
http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html