sed:shell
模版:ide
The honeysuckle band played all night long for noly $90.spa
It was an evening of splendid music and company.ip
Too bad the disco floor fell through at 23:10.md5
The local nurse Miss P.Neaue was in attendance.文檔
sed選項以下:input
n 不打印;sed不寫編輯行到標準輸出,缺省爲打印全部行(編輯和未編輯) 。p命令能夠用來打印編輯行。(重點)it
c 下一命令是編輯命令。使用多項編輯時加入此選項。若是隻用到一條 sed命令,此選項無用,但指定它也沒有關係。class
f 若是正在調用s e d腳本文件,使用此選項。此選項通知 sed一個腳本文件支持全部的 sed命令,例如:sed -f myscript.sed input_file,test
這裏myscript.sed即爲支持sed命令的文件。
使用sed在文件中定位文本的方式:
x:x爲一行號,如1。
x,y:表示行號範圍從x到y,如2,5表示從第2行到第5行。
/pattern/:查詢包含模式的行。例如/disk/或/[a-z]/。
/patern/:pattern/查詢包含兩個模式的行。例如/disk/disks/。
pattern/,x:在給定行號上查詢包含模式的行。如 /ribbon/,3。
x,/pattern/:經過行號和模式查詢匹配行。3./vdu/。
x,y!:查詢不包含指定行號x和y的行。1,2 !。
sed編輯命令:
p 打印匹配行
= 顯示文件行號
a \ 在定位行號後附加新文本信息
i \ 在定位行號後插入新文本信息
d 刪除定位行
c \ 用新文本替換定位文本
s 使用替換模式替換相應模式
r 從另外一個文件中讀文本
w 寫文本到一個文件
q 第一個模式匹配完成後推出或當即推出
l 顯示與八進制A S C I I代碼等價的控制字符
{ } 在定位行執行的命令組
n 從另外一個文件中讀文本下一行,並附加在下一行
g 將模式2粘貼到/pattern n/
y 傳送字符
n 延續到下一輸入行;容許跨行的模式匹配語句
顯示指定行:
例:[root@localhost sed]# sed '2p' data.f
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
例:[root@localhost sed]# sed -n '2p' data.f
It was an evening of splendid music and company.
釋:若是不加-n,sed就不能定義文件行數。因此打印了所有信息。
顯示範圍行:
例:[root@localhost sed]# sed -n '1,3p' data.f
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
釋:顯示1到3行數據。
顯示行號:
[root@CMN-XN-4-3g1 yingyou]# sed '/floor/=' test
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
3
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
[root@CMN-XN-4-3g1 yingyou]# sed -n '/floor/=' test
3
釋:不加-n選項則顯示所有和相應行號
替換:
[root@CMN-XN-4-3g1 yingyou]# sed 's/\$//g' test
The honeysuckle band played all night long for noly 90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
[root@CMN-XN-4-3g1 yingyou]# sed 's/in/ABCD/w 123.txt' test
The honeysuckle band played all night long for noly $90.
It was an evenABCDg of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was ABCD attendance.
[root@CMN-XN-4-3g1 yingyou]# cat 123.txt
It was an evenABCDg of splendid music and company.
The local nurse Miss P.Neaue was ABCD attendance.
釋:只是將替換的結果存到文件中,不會將所有都放進去。
[root@CMN-XN-4-3g1 yingyou]# sed -n 's/fell/"abcd" &/p' test
Too bad the disco floor "abcd" fell through at 23:10.
[root@CMN-XN-4-3g1 yingyou]# sed -n 's/fell/abcd &/p' test
Too bad the disco floor abcd fell through at 23:10.
釋:匹配模式前插入你要加入的字符。
寫入:
root@localhost sed]# sed '/bad/ w 456.txt' test
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
[root@localhost sed]# cat 456.txt
Too bad the disco floor fell through at 23:10.
[root@localhost etc]# sed -n '14,24 w named.conf' rndc.conf
[root@localhost etc]# cat named.conf
# Use with the following in named.conf, adjusting the allow list as needed:
# key "rndckey" {
# algorithm hmac-md5;
# secret "Dqx3HzhRkD9X2cyL6pnLvQ==";
# };
#
# controls {
# inet 127.0.0.1 port 953
# allow { 127.0.0.1; } keys { "rndckey"; };
# };
# End of named.conf
釋:能夠打印行號內容,注意這樣作會覆蓋原有數據。
[root@localhost sed]# sed '1,3 w 456.txt' test
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
[root@localhost sed]# cat 456.txt
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
釋:能夠匹配正則內容
[root@localhost sed]# sed '/23:10./r 123.txt' test
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
abcdefg
The local nurse Miss P.Neaue was in attendance.
釋:用r選項正則匹配行下加入123.txt文件的內容
模式匹配首次出現後退出sed
[root@localhost sed]# sed '/\./q' test
The honeysuckle band played all night long for noly $90.
[root@localhost sed]# sed '/.a.*/q' test
The honeysuckle band played all night long for noly $90.
釋:假設要在test文檔中查找「.「或「.a.*」可是要在首次匹配到就須要退出則使用q參數。
綜合聯繫
[root@localhost sed]# cat test
The honeysuckle band played all night long for noly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
---------------------------------------------
fasdfds
fsdfs
-----------------------------------------------
safsdfdsfsdf
sfsdfsd
[root@localhost sed]# sed 's/-*//g' test |sed '/^$/d' |sed '$d' |sed '1d'
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neaue was in attendance.
fasdfds
fsdfs
safsdfdsfsdf
釋:第一命令是刪掉虛線,第二命令是刪掉空行,第三命令是刪除最後一行,第四個命令是刪除第一行
去除行首:
[root@localhost sed]# cat 123.txt
121 UDP_TCP
121 UDP_TCP
223 UDP_TCP
2 UDP_TCP
4324 UDP_TCP
32 UDP_TCP
543 UDP_TCP
534 UDP_TCP
654 UDP_TCP
654 UDP_TCP
[root@localhost sed]# sed 's/^[0-9]*//g' 123.txt
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
UDP_TCP
添加行尾:
[root@localhost sed]# sed 's/[0-9]*/& UDP/w 678.txt' 456.txt
121 UDP
121 UDP
223 UDP
2 UDP
4324 UDP
32 UDP
543 UDP
534 UDP
654 UDP
654 UDP
[root@localhost sed]# cat 678.txt
121 UDP
121 UDP
223 UDP
2 UDP
4324 UDP
32 UDP
543 UDP
534 UDP
654 UDP
654 UDP
從shell向sed傳值:
[root@localhost sed]# echo $name |sed "s/the/$ABCD/g"
Too bad ABCD disco floor fell through at 23:10\.
[root@localhost sed]# echo $name
Too bad the disco floor fell through at 23:10\.
[root@localhost sed]# echo $ABCD
ABCD
釋:注意sed符號要用雙引號。
從sed輸出中設置shell變量:
[root@localhost sed]# echo $name
Too bad the disco floor fell through at 23:10\.
[root@localhost sed]# echo $ABCD
ABCD
[root@localhost sed]# NEW_NAME=`echo $name |sed "s/the/$ABCD/g"`
[root@localhost sed]# echo $NEW_NAME
Too bad ABCD disco floor fell through at 23:10\.
‘s/\.$/ /g’ 刪除以句點結尾行
‘-e/abcd/d’ 刪除包含abcd的行
‘s/[ ][ ][ ]*/[ ]/g’ 刪除一個以上空格,用一個空格代替
‘s/^[ ][ ] * / / g’ 刪除行首空格
‘s/\.[ ][ ]*/[ ]/g’ 刪除句點後跟兩個或更多空格,代之以一個空格
‘/^$/d’ 刪除空行
‘s/^./ /g’ 刪除第一個字符
‘s/COL\( ...\)/ /g’ 刪除緊跟C O L的後三個字母
‘s/^ \/ / /g’ 從路徑中刪除第一個\
‘s/[ ]/[ ]/ /g’ 刪除全部空格並用t a b鍵替代
‘S/^[ ] / /g’ 刪除行首全部t a b鍵
‘s/[ ] * / /g’ 刪除全部t a b鍵