【現學現忘&Shell編程】— 36.sed命令(二)

(4)向文件中插入數據

需求:在student.txt文本中第三行前面添加88888888888

執行命令如下:

[root@localhost tmp]# sed "3i 88888888888888" student.txt 
ID  Name    Python  Linux   MySQL   Java
1   Tangs   88      87      86      85.55
88888888888888
2   Sunwk   99      98      97      96.66
66666666666666
3   Zhubj   77      76      75      74.44
4   Shahs   66      65      64      63.33

同理,需要把修改寫入文件,需要添加-i選項。

(5)修改文件中的多行數據(刪除,追加,插入)

如果是想追加或插入多行數據,除最後一行外,每行的末尾都要加入\代表數據未完結。

需求:向student.txt文本中第二行後面追加hello world

執行如下命令:

[root@localhost tmp]# sed '2a hello \
> world' student.txt
ID  Name    Python  Linux   MySQL   Java
1   Tangs   88      87      86      85.55
hello 
world
2   Sunwk   99      98      97      96.66
3   Zhubj   77      76      75      74.44
4   Shahs   66      65      64      63.33

(引號不完全是不會執行的)

提示:我發現換行後tab鍵提示功能不能用了。(不清楚怎麼回事)

這裏在說明一下-n選項,-n選項只會把經過sed命令處理的行輸出到屏幕。

執行如下命令:

[root@localhost tmp]# sed -n '2a hello \
world' student.txt
hello 
world

(6)替換文件中的整行文本

需求:替換student.txt文本中的第二行數據爲999999999999999

執行如下命令:

[root@localhost tmp]# sed '2c 999999999999999' student.txt 
ID  Name    Python  Linux   MySQL   Java
999999999999999
2   Sunwk   99      98      97      96.66
3   Zhubj   77      76      75      74.44
4   Shahs   66      65      64      63.33

注意:sed命令默認情況是不會修改文件內容的,如果確定需要讓sed命令直接處理文件的內容,可以使用-i選項。不過要小心啊,這樣非常容易誤操作,在操作系統文件時請小心謹慎。

(7)字符串替換

sedc動作是進行整行替換的,如果僅僅想替換行中的部分數據,就要使用s動作了。

需求:修改Zhubj的Java成績爲100

執行如下命令:

# 命令格式
[root@localhost tmp]# sed 's/舊字串/新字串/g' 文件名

# 執行命令
[root@localhost tmp]# sed 's/74.44/100/g' student.txt 
ID  Name    Python  Linux   MySQL   Java
1   Tangs   88      87      86      85.55
2   Sunwk   99      98      97      96.66
3   Zhubj   77      76      75      100
4   Shahs   66      65      64      63.33

# 或者對行範圍更精準一些
[root@localhost tmp]# sed '4s/74.44/100/g' student.txt 
ID  Name    Python  Linux   MySQL   Java
1   Tangs   88      87      86      85.55
2   Sunwk   99      98      97      96.66
3   Zhubj   77      76      75      100
4   Shahs   66      65      64      63.33

注意:

4s/74.44/100/g表達式中s/之間不能有空格。

如果4s/74.44/100/g不寫行號,也就是上面第一種寫法,就代表替換整個文檔中的匹配字符串。

(8)同時執行多條動作

sed命令中,-e選項可以同時執行多個sed動作,當然如果只是執行一個動作也可以使用-e選項,但是這時-e選項是沒有什麼意義的。

還要注意多個動作之間要用;號或回車分割。

練習1:

需求:把Shahs的成績註釋掉,並且把Zhubj的Python成績改成100。

執行命令如下:

# 使用;分號的方式隔離多條動作
[root@localhost tmp]# sed -e '5s/^/#/g ; 4s/77/100/g' student.txt 
ID  Name    Python  Linux   MySQL   Java
1   Tangs   88      87      86      85.55
2   Sunwk   99      98      97      96.66
3   Zhubj   100     76      75      74.44
#4  Shahs   66      65      64      63.33

注意:

^代表行首,是正則表達式,不用加-r選項也可以。

其實上面命令中不寫-e選項,命令也是可以執行的,應該是默認識別的。

但是我們儘量規範書寫。

# 使用回車的方式隔離多條動作
[root@localhost tmp]# sed -e '5s/^/#/g 
> 4s/77/100/g' student.txt
ID  Name    Python  Linux   MySQL   Java
1   Tangs   88      87      86      85.55
2   Sunwk   99      98      97      96.66
3   Zhubj   100     76      75      74.44
#4  Shahs   66      65      64      63.33

注意:使用回車的方式進行動作之間的分隔,就不能在寫分號;了。

特別注意:

sed命令中有多條動作執行的時候,且有多個選項的時候,-e選項要緊挨動作表達式,否則會報錯。

例如:

-i -e '5s/^/#/g'正確,

-e -i '5s/^/#/g'報錯。

練習2:

刪除字符串使用sed命令的d操作是不能實現的,因爲d操作是刪除整行。這時就需要字符串替換,也就是sed命令的s動作來完成。

需求:刪除Zhubj的Python成績。

執行如下命令:

# 用空代替
[root@localhost tmp]# sed '4s/77//g' student.txt 
ID  Name    Python  Linux   MySQL   Java
1   Tangs   88      87      86      85.55
2   Sunwk   99      98      97      96.66
3   Zhubj           76      75      74.44
4   Shahs   66      65      64      63.33

3、總結

什麼時候需要使用sed命令?

如果需要在腳本程序中,修改文件中的數據,這種情況下我們就需要使用sed命令。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章