linux-shell編程6:sed練習

sed案例

#源文件
[root@localhost ~]# cp  /etc/sysconfig/network-scripts/ifcfg-eno16777728 test.txt

#第二行後追加TYPE=Ethernet
    [root@localhost ~]# sed '2a TYPE=Ethernet' test.txt
#第三行前插入TYPE=Ethernet
    sed '3i TYPE=Ethernet' test.txt
#將文本中所有的yes替換爲no
    [root@localhost ~]# sed 's/yes/no/g' test.txt
#刪除第三四行的內容
    [root@localhost ~]# sed  '3,4d' test.txt

sed命令集練習

  • 案例文件

[root@localhost ~]# cat quote.txt
The honeysuckle band played all night long for inly $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.Neave was in attendance.

使用p(rint)顯示行

[root@localhost ~]# sed '2p' quote.txt 
    The honeysuckle band played all night long for inly $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.Neave was in attendance.
[root@localhost ~]# sed -n '2p' quote.txt 
    It was an evening of splendid music and company.
[root@localhost ~]# 

打印範圍

number:指定輸入文件的唯一行號
number1,number2:'1,3p' 匹配number1到number2支間的所有行
first~step:指定從first行開始,每個step幾行打印
$:代表最後一行
addr1,+N:匹配addr1以及後面的N行內容
/regexp/ 匹配正則

[root@localhost ~]# sed -n '1,3'p quote.txt 
[root@localhost ~]# sed -n '1~2p' quote.txt 
[root@localhost ~]# sed -n '1,+1'p quote.txt 

打印匹配模式

/pattern/:匹配模式
#匹配單詞evening,並打印此行
[root@localhost ~]# sed -n '/evening/p' quote.txt 
It was an evening of splendid music and company.
[root@localhost ~]# 

使用模式和行號進行查詢

在查找時,sed返回指定單詞的許多行,爲了返回結果更加精確,我們可以將行號和模式進行結合
使用模式與行號混合方法時:格式爲:
line_number,/patter/   #行號與匹配逗號隔開
[root@localhost ~]# sed -n '/The/p' quote.txt
[root@localhost ~]# sed -n '4,/The/p' quote.txt

匹配元字符

$
對於元字符,使用轉義字符\屏蔽其原有含義
/\$/p

[root@localhost ~]# sed -n '/\$/'p quote.txt

顯示整個文件

從第一行匹配到最後一行    1,$

[root@localhost ~]# sed -n '1,$'p quote.txt

顯示任意字符

匹配任意字母,
.*ing

[root@localhost ~]# sed -n '/.*ing/'p quote.txt

打印首行

打印文件第一行
sed -n '1p'  quote.txt

打印最後一行

sed -n '$'p  quote.txt

打印行號

/pattern/=

[root@localhost ~]# sed -n '/music/=' quote.txt 
2
[root@localhost ~]# sed -n '/music/p' quote.txt 
It was an evening of splendid music and company.
[root@localhost ~]# sed -n -e '/musci/p' -e '/musci/=' quote.txt 
[root@localhost ~]# sed -n -e '/music/p' -e '/music/=' quote.txt 
It was an evening of splendid music and company.
2
[root@localhost ~]# cat -n quote.txt 
     1  The honeysuckle band played all night long for inly $90.
     2  It was an evening of splendid music and company.
     3  Too bad the disco floor fell through at 23:10.
     4  The local nurse MIss P.Neave was in attendance.
[root@localhost ~]# sed -e '/music/=' quote.txt 
The honeysuckle band played all night long for inly $90.
2
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse MIss P.Neave was in attendance.
[root@localhost ~]# 

文本追加

a/  附加文本
可以指定文本一行或多行附加到指定行,若不指定文本放置位置,缺省放置在每一行後面
格式:
    [address]a\text\text\text\

    root@localhost ~]# sed "/company/a\I love Beijing Tian'anmen " quote.txt 

    [root@localhost ~]# sed "a\I love Beijing Tian'anmen " quote.txt 

利用腳本進行追加

[root@localhost ~]# vim add.sed
[root@localhost ~]# cat add.sed 
#!/bin/sed -f
/company/a\
I love my gril friend. \
I love myself.
[root@localhost ~]# 

[root@localhost ~]# chmod u+x add.sed 
[root@localhost ~]# ./add.sed 

[root@localhost ~]# ./add.sed  quote.txt 
The honeysuckle band played all night long for inly $90.
It was an evening of splendid music and company.
I love my gril friend. 
I love myself.
Too bad the disco floor fell through at 23:10.
The local nurse MIss P.Neave was in attendance.
[root@localhost ~]# 

插入文本

i/  插入文本
插入文本類似於附加文本,只是在指定行前插入
[root@localhost ~]# vim insert.sed
[root@localhost ~]# cat insert.sed 
#!/bin/sed -f
4 i\
Welcome to Beijing Tu Ling XueYuan 
[root@localhost ~]# 

[root@localhost ~]# chmod u+x insert.sed 
[root@localhost ~]# ./insert.sed quote.txt 
The honeysuckle band played all night long for inly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
Welcome to Beijing Tu Ling XueYuan 
The local nurse MIss P.Neave was in attendance.
[root@localhost ~]# 

修改文本

[address[,address]] c\
text/
text/
………………

[root@localhost ~]# vim cha.sed
[root@localhost ~]# chmod +x cha.sed 
[root@localhost ~]# ./cha.sed quote.txt 
北京圖靈學院歡迎您!
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse MIss P.Neave was in attendance.
[root@localhost ~]# cat -n quote.txt 
     1  The honeysuckle band played all night long for inly $90.
     2  It was an evening of splendid music and company.
     3  Too bad the disco floor fell through at 23:10.
     4  The local nurse MIss P.Neave was in attendance.
[root@localhost ~]# cat -n cha.sed 
     1  #!/bin/sed -f
     2  /honeysuckle/ c\
     3  北京圖靈學院歡迎您!
[root@localhost ~]# 

針對於同一個文件可以對腳本進行混合操作

[root@localhost ~]# vim Supper.sed
[root@localhost ~]# 
[root@localhost ~]# cat Supper.sed 
#!/bin/sed -f
1 c\
北京圖靈學院歡迎您~~~

/evening/  i\
歡迎來到北京圖靈學院學習~~~

3 a\
我們的課程包含python,java,數據分析,前端,php,linux

$ c\
北京圖靈學院祝願學業有成~~
[root@localhost ~]# chmod u+x Supper.sed 
[root@localhost ~]# ./Supper.sed quote.txt 
北京圖靈學院歡迎您~~~
歡迎來到北京圖靈學院學習~~~
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
我們的課程包含python,java,數據分析,前端,php,linux
北京圖靈學院祝願學業有成~~
[root@localhost ~]# 

刪除文本

格式:
[address[,address]]d   #可以是的行也可以是匹配

刪除第一行:
刪除最後一行:
使用正則進行刪除:

[root@localhost ~]# sed '1d' quote.txt 
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse MIss P.Neave was in attendance.
[root@localhost ~]# sed '$d' quote.txt 
The honeysuckle band played all night long for inly $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
[root@localhost ~]# sed '/music/d' quote.txt 
The honeysuckle band played all night long for inly $90.
Too bad the disco floor fell through at 23:10.
The local nurse MIss P.Neave was in attendance.
[root@localhost ~]# 

替換文本

格式:
[address[,address]] s\ x/y/[gpwn]

s  通知sed爲替換操作,查詢x,替換爲y

g   全局替換,默認替換第一次匹配到的
p   標準輸出,加p使-n不生效
w   替換輸出文件爲,另存爲

#替換company爲COMPANY
[root@localhost ~]# sed 's/company/CONMPANY/' quote.txt

#替換小寫the
[root@localhost ~]# sed 's/The/the/' quote.txt 

[root@localhost ~]# sed 's/The/the/' quote.txt 
the The honeysuckle band played all night long for inly $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.Neave was in attendance.
[root@localhost ~]# sed 's/The/the/g' quote.txt 
the the honeysuckle band played all night long for inly $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.Neave was in attendance.
[root@localhost ~]# 

#從$90中刪除$符號
[root@localhost ~]# sed 's/\$//' quote.txt 
The The honeysuckle band played all night long for inly 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.Neave was in attendance.
[root@localhost ~]# 

#文件另存爲
w 存儲數據文件
[root@localhost ~]# sed 's/splendid/HELLO/' quote.txt  > sed.out

[root@localhost ~]# sed 's/splendid/HELLO/w' quote.txt
sed: couldn't open file : No such file or directory
[root@localhost ~]# sed 's/splendid/HELLO/w sed.out' quote.txt
The The honeysuckle band played all night long for inly $90.
It was an evening of HELLO music and company.
Too bad the disco floor fell through at 23:10.
The local nurse MIss P.Neave was in attendance.
[root@localhost ~]# cat sed.out 
It was an evening of HELLO music and company.
[root@localhost ~]# 

使用替換修改字符串

如果要附加或修改一個字符串,可以使用(&)命令
&命令保存發現模式,以便重新調用,然後把他放在替換字符串裏面
先給出一個被替換模式,然後是一個準備附加在第一個模式後的另一個模式,並且後面帶有&,這樣修改模式將放在匹配模式之前

[root@localhost ~]# sed -n 's/floor/FLLOR &/p' quote.txt
[root@localhost ~]# sed -n 's/band/ Welcome  TLXY &/'p quote.txt

寫入文件

使用 > 文件重定向
[root@localhost ~]# 
[root@localhost ~]# sed '1,2 w file' quote.txt 
The The honeysuckle band played all night long for inly $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.Neave was in attendance.
[root@localhost ~]# cat fil
file    filedt  filrft  
[root@localhost ~]# cat file
The The honeysuckle band played all night long for inly $90.
It was an evening of splendid music and company.
[root@localhost ~]# 

從文件中讀取文本

格式:
    address r  filename

[root@localhost ~]# vim r.txt
[root@localhost ~]# cat r.txt 
I love my girl friend
[root@localhost ~]# sed '/company/r r.txt' quote.txt 
The The honeysuckle band played all night long for inly $90.
It was an evening of splendid music and company.
I love my girl friend
Too bad the disco floor fell through at 23:10.
The local nurse MIss P.Neave was in attendance.
[root@localhost ~]# sed '$r r.txt' quote.txt 
The The honeysuckle band played all night long for inly $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.Neave was in attendance.
I love my girl friend
[root@localhost ~]# 

匹配後退出

q
有時候需要在模式匹配首次出現後退出,以便執行其他腳本指令
格式:
    address q

[root@localhost ~]# cat q.txt 
Line 1.band
Line 2.bad
Line 3.was
Line 4.was
[root@localhost ~]#
[root@localhost ~]# sed '/.*a.*/q' q.txt 
Line 1.band
[root@localhost ~]# 

去除首行數字

[root@localhost ~]# cat a.txt 
121Line 1.band
3232Line 2.bad
342345234Line 3.was
342342Line 4.was
[root@localhost ~]# 

[root@localhost ~]# sed 's/^[0-9]*/Start /g' a.txt 
Start Line 1.band
Start Line 2.bad
Start Line 3.was
Start Line 4.was
[root@localhost ~]# sed 's/^[0-9]*//g' a.txt 
Line 1.band
Line 2.bad
Line 3.was
Line 4.was
[root@localhost ~]# 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章