三剑客之sed常用操作
Sed
Sed是一個強(qiáng)大的文本處理工具
可以采用正則匹配,對文本進(jìn)行插入刪除修改等操作
Sed處理的時候,一次處理一行,每一次把當(dāng)前處理的存放在臨時緩沖區(qū),處理完后輸出緩沖區(qū)內(nèi)容到屏幕,然后把下一行讀入緩沖區(qū),如此重復(fù),直到結(jié)尾。
1、命令格式和參數(shù)
sed [-nefr] [動作] 文件
參數(shù):
-n 安靜模式,在sed處理的時候,所有來自STDIN的數(shù)據(jù)都會被輸出到終端,加上-n會只輸出處理的哪行
-e 直接在命令列上進(jìn)行sed動作編輯
-f 直接將sed的動作寫在文件內(nèi)
-r sed動作支持延伸的正則表達(dá)(默認(rèn)只是基礎(chǔ)正則)
-i 直接修改文件內(nèi)容(慎用,尤其是用系統(tǒng)文件做練習(xí)的時候)
動作:
a?append:增加,在當(dāng)前行的下一行增加
c ? :取代,取代n1到n2之間的行
d delete:刪除
i 插入,目前行的上一行插入
p 打印,常常與-n使用
s 取代,s/old/new/g
2、基礎(chǔ)用法詳解
(1)第一行之后添加一行
[root@localhost?~]#?nl?file.txt?|?sed?"1a?add?text"??
?????1??wtmp?begins?Mon?Feb?24?14:26:08?2014??
add?text??
?????2??192.168.0.1??
?????3??162.12.0.123??
?????4??this?is?the?last?line??
(2)第一行之前添加一行?copy
[root@localhost?~]#?nl?file.txt?|?sed?"1i?add?text"??
add?text??
?????1??wtmp?begins?Mon?Feb?24?14:26:08?2014??
?????2??192.168.0.1??
?????3??162.12.0.123??
?????4??this?is?the?last?line??
(3)刪除第2,3行
[root@localhost?~]#?nl?file.txt?|?sed?"2,3d"??
?????1??wtmp?begins?Mon?Feb?24?14:26:08?2014??
?????4??this?is?the?last?line??
(4)打印第2,3行?copy
[root@localhost?~]#?sed?-n?"2,3p"?file.txt???
192.168.0.1??
162.12.0.123??
這里要提到的是,盡量使用-n,不然會出現(xiàn)這樣的結(jié)果?copy
[root@localhost?~]#?sed?"2,3p"?file.txt???
wtmp?begins?Mon?Feb?24?14:26:08?2014??
192.168.0.1??
192.168.0.1??
162.12.0.123??
162.12.0.123??
this?is?the?last?line??
(5)把168換成169
先看源文件?copy
[root@localhost?~]#?cat?file.txt???
wtmp?begins?Mon?Feb?24?14:26:08?2014??
192.168.0.1??
162.12.0.123??
this?is?the?last?line??
處理后?copy
[root@localhost?~]#?sed?"s/168/169/g"?file.txt???
wtmp?begins?Mon?Feb?24?14:26:08?2014??
192.169.0.1??
162.12.0.123??
this?is?the?last?line??
(6)插入多行
[root@localhost?~]#?nl?file.txt?|?sed?"2afirst\nsecond"?file.txt???
wtmp?begins?Mon?Feb?24?14:26:08?2014??
192.168.0.1??
first??
second??
162.12.0.123??
this?is?the?last?line??
(7)匹配數(shù)據(jù),然后進(jìn)行操作
只需要在上述的基礎(chǔ)上加上正則匹配
sed "/匹配的模式/處理的方式" file.txt?
sed "/^root/d" file.txt 對開始有root的刪除
例如
匹配begin,并刪除改行?copy
[root@localhost?~]#?nl?file.txt?|?sed?"/begin/d"??
?????2??192.168.0.1??
?????3??162.12.0.123??
?????4??this?is?the?last?line??
匹配123,并且把含有123的行162都替換成172?copy
[root@localhost?~]#?nl?file.txt?|?sed?"/123/{s/162/172/g;q}"??
?????1??wtmp?begins?Mon?Feb?24?14:26:08?2014??
?????2??192.168.0.1??
?????3??172.12.0.123??
?????4??this?is?the?last?line??
這里大括號{}里可以執(zhí)行多個命令,用;隔開即可,q是退出
(8)連續(xù)編輯 -e
刪除第二行,并且匹配把last替換成new
<pre?name="code"?class="plain">[root@localhost?~]#?nl?file.txt?|?sed?-e?"2d"?-e?"s/last/new/"??
?????1??wtmp?begins?Mon?Feb?24?14:26:08?2014??
?????3??162.12.0.123??
?????4??this?is?the?new?line??
(9)直接修改文件,切記不要修改系統(tǒng)文件?copy
[root@localhost?~]#?sed?-i?"/begin/{s/24/25/g}"?file.txt???
[root@localhost?~]#?cat?file.txt???
wtmp?begins?Mon?Feb?25?14:26:08?2014??
192.168.0.1??
162.12.0.123??
this?is?the?last?line??
三 、一個比較有趣的例子
如何替換\n也就是把所有的行都?xì)w為一行
第一種方式?copy
[root@localhost?~]#?sed?':a;N;$!ba;s/\n/?/g'?file.txt???
wtmp?begins?Mon?Feb?25?14:26:08?2014?192.168.0.1?162.12.0.123?this?is?the?last?line??
第二種方式opy
[root@localhost?~]#?tr?"\n"?"?"?<?file.txt???
wtmp?begins?Mon?Feb?25?14:26:08?2014?192.168.0.1?162.12.0.123?this?is?the?last?line?last?linen??
轉(zhuǎn)載于:https://blog.51cto.com/jschu/2120833
總結(jié)
以上是生活随笔為你收集整理的三剑客之sed常用操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5月19日工作日志
- 下一篇: 多线程threading初识,线程等待