Linux文件比較三劍客(awk/grep/sed)之sed

一、什么是sed


sed是一個很好的文件處理工具,本身是一個管道命令,主要是以行為單位進行處理,可以將數(shù)據(jù)行進行替換、刪除、新增、選取等特定工作。

二、sed命令格式和選項


·2.1 語法形式
命令 功能描述
a| 在當前行的后面加入一行或者文本。
c| 用新的文本改變或者替代本行的文本。
d 從pattern space位置刪除行。
i| 在當前行的上面插入文本。
h 拷貝pattern space的內容到holding buffer(特殊緩沖區(qū))。
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 spacefile的末尾。
! 表示后面的命令對所有沒有被選定的行發(fā)生作用。
s/re/string string替換正則表達式re
= 打印當前行號碼。
替換標記 -
g 行內全面替換,如果沒有g,只替換第一個匹配。
p 打印行。
x 互換pattern spaceholding 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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容