天天看點

shell腳本三劍客之sed指令

sed:

模版:

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指令,此選項無用,但指定它也沒有關系。

f 如果正在調用s e d腳本檔案,使用此選項。此選項通知 sed一個腳本檔案支援所有的 sed指令,例如:sed -f myscript.sed input_file,

這裡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 延續到下一輸入行;允許跨行的模式比對語句

顯示指定行:

例:[[email protected] 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.

例:[[email protected] sed]# sed -n '2p' data.f 

It was an evening of splendid music and company.

釋:如果不加-n,sed就不能定義檔案行數。是以列印了全部資訊。

顯示範圍行:

例:[[email protected] 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行資料。

顯示行号:

[[email protected] 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.

[[email protected] yingyou]# sed -n '/floor/=' test 

3

釋:不加-n選項則顯示全部和相應行号

替換:

[[email protected] 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.

[[email protected] 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.

[[email protected] yingyou]# cat 123.txt 

It was an evenABCDg of splendid music and company.

The local nurse Miss P.Neaue was ABCD attendance.

釋:隻是将替換的結果存到檔案中,不會将全部都放進去。

[[email protected] yingyou]# sed -n 's/fell/"abcd" &/p' test 

Too bad the disco floor "abcd" fell through at 23:10.

[[email protected] yingyou]# sed -n 's/fell/abcd &/p' test 

Too bad the disco floor abcd fell through at 23:10.

釋:比對模式前插入你要加入的字元。

寫入:

[email protected] 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.

[[email protected] sed]# cat 456.txt 

Too bad the disco floor fell through at 23:10.

[[email protected] etc]# sed -n '14,24 w named.conf' rndc.conf 

[[email protected] 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

釋:可以列印行号内容,注意這樣做會覆寫原有資料。

[[email protected] 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.

[[email protected] 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.

釋:可以比對正則内容

[[email protected] 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

[[email protected] sed]# sed '/\./q' test 

The honeysuckle band played all night long for noly $90.

[[email protected] sed]# sed '/.a.*/q' test 

The honeysuckle band played all night long for noly $90.

釋:假設要在test文檔中查找“.“或“.a.*”但是要在首次比對到就需要退出則使用q參數。

綜合聯系

[[email protected] 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

[[email protected] 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

釋:第一指令是删掉虛線,第二指令是删掉空行,第三指令是删除最後一行,第四個指令是删除第一行

去除行首:

[[email protected] 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

[[email protected] 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

添加行尾:

[[email protected] 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

[[email protected] 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傳值:

[[email protected] sed]# echo $name |sed "s/the/$ABCD/g"

Too bad ABCD disco floor fell through at 23:10\.

[[email protected] sed]# echo $name

Too bad the disco floor fell through at 23:10\.

[[email protected] sed]# echo $ABCD

ABCD

釋:注意sed符号要用雙引号。

從sed輸出中設定shell變量:

[[email protected] sed]# echo $name

Too bad the disco floor fell through at 23:10\.

[[email protected] sed]# echo $ABCD

ABCD

[[email protected] sed]# NEW_NAME=`echo $name |sed "s/the/$ABCD/g"`

[[email protected] 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鍵

轉載于:https://blog.51cto.com/huanglihuilinux/1719667

繼續閱讀