sed:
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供柳州網(wǎng)站建設(shè)、柳州做網(wǎng)站、柳州網(wǎng)站設(shè)計、柳州網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、柳州企業(yè)網(wǎng)站模板建站服務(wù),十載柳州做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。模版:
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.
sed選項如下:
n 不打??;sed不寫編輯行到標準輸出,缺省為打印所有行(編輯和未編輯) 。p命令可以用來打印編輯行。(重點)
c 下一命令是編輯命令。使用多項編輯時加入此選項。如果只用到一條 sed命令,此選項無用,但指定它也沒有關(guān)系。
f 如果正在調(diào)用s e d腳本文件,使用此選項。此選項通知 sed一個腳本文件支持所有的 sed命令,例如:sed -f myscript.sed input_file,
這里myscript.sed即為支持sed命令的文件。
使用sed在文件中定位文本的方式:
x:x為一行號,如1。
x,y:表示行號范圍從x到y(tǒng),如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 使用替換模式替換相應(yīng)模式
r 從另一個文件中讀文本
w 寫文本到一個文件
q 第一個模式匹配完成后推出或立即推出
l 顯示與八進制A S C I I代碼等價的控制字符
{ } 在定位行執(zhí)行的命令組
n 從另一個文件中讀文本下一行,并附加在下一行
g 將模式2粘貼到/pattern n/
y 傳送字符
n 延續(xù)到下一輸入行;允許跨行的模式匹配語句
顯示指定行:
例:[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就不能定義文件行數(shù)。所以打印了全部信息。
顯示范圍行:
例:[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行數(shù)據(jù)。
顯示行號:
[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選項則顯示全部和相應(yīng)行號
替換:
[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.
釋:只是將替換的結(jié)果存到文件中,不會將全部都放進去。
[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
釋:可以打印行號內(nèi)容,注意這樣做會覆蓋原有數(shù)據(jù)。
[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.
釋:可以匹配正則內(nèi)容
[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文件的內(nèi)容
模式匹配首次出現(xiàn)后退出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.
釋:假設(shè)要在test文檔中查找“.“或“.a.*”但是要在首次匹配到就需要退出則使用q參數(shù)。
綜合聯(lián)系
[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輸出中設(shè)置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’ 刪除以句點結(jié)尾行
‘-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鍵
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。