liunx之sed命令詳解

簡介

sed全稱是:Stream EDitor,是一個很好的文件處理工具

使用語法

sed [option] 'command' file_path

常用選項option(可選)

  • -i 直接修改讀取的文件內容,而不是由屏幕輸出。
  • -f 直接將 sed 的動作寫在一個文件內, -f filename 則可以執行filename內的sed命令;
  • -n 使用安靜(silent)模式。在一般sed的用法中,所有來自stdin的內容一般都會被列出到屏幕上。但如果加上-n參數後,則只有經過sed特殊處理的那一行(或者動作)纔會被列出來;
  • -e 直接在指令列模式上進行 sed 的動作編輯;
  • -r 讓sed命令支持擴展的正則表達式(默認是基礎正則表達式);

常用命令command

  • [range]a string∶append即追加字符串string, a 的後面可以接字串string(多行字符串可以用\n分隔),而這些字串將追加到每個匹配行的下一行,range表範圍,可以用數字區間(n[,m])表示,也可以用正則(/pattern/)

  • [range]i string∶insert即插入字符串, a 的後面可以接字串string(多行字符串可以用\n分隔),而這些字串將追加到每個匹配行的上一行,range表範圍,可以用數字區間(n[,m])表示,也可以用正則(/pattern/)

  • [range]c string∶取代, c 的後面可以接字串(多行字符串可以用\n分隔),而這些字串將替換到匹配的行,range表範圍,可以用數字區間(n[,m])表示,也可以用正則(/pattern/)

  • s: 替換,通常這個 s 的動作可以搭配正規表示法!例如:1,2s/old/new/g,將old字符串替換成new字符串

  • [range]d∶delete即刪除,刪除指定範圍的內容。range表範圍(不指定範圍表示所有內容),可以用數字區間(n[,m])表示,也可以用正則(/pattern/)

  • [range]p∶print即打印,打印出指定範圍的資料。通常 p 會與參數 sed -n 一起運作。range表範圍(不指定範圍表示所有內容),可以用數字區間(n[,m])表示,也可以用正則(/pattern/)

實例:

假設有一個本地文件test.txt,文件內容如下:

[root@localhost ~]# cat test.txt
my name is kwin
my email address is kwinwong@hotmail.com
my blog is http://blog.csdn.net/kwinh
Please contact me if you have any questions
end

a 追加

追加指定單行

[root@localhost ~]# sed '1a hello' test.txt
my name is kwin
hello
my email address is kwinwong@hotmail.com
my blog is http://blog.csdn.net/kwinh
Please contact me if you have any questions
end

本例命令部分中的1表示第一行,同樣的第二行寫成2,第一行到第三行寫成1,3,用2, 表示第二行到最後一行中間所有的行(包含第二行和最後一行)。

範圍匹配追加

[root@localhost ~]# sed '1,$a hello' test.txt
my name is kwin
hello
my email address is kwinwong@hotmail.com
hello
my blog is http://blog.csdn.net/kwinh
hello
Please contact me if you have any questions
hello
end
hello

本例表示在所有的行後面都加上”hello”字符串,從輸出可以看到效果。同sed 'a hello' test.txt

追加指定正則匹配行

[root@localhost ~]# sed '/^my.*com$/a hello' test.txt
my name is kwin
my email address is kwinwong@hotmail.com
hello
my blog is http://blog.csdn.net/kwinh
Please contact me if you have any questions
end

本例使用正則表達式匹配行,^my.*com$表示以my開頭,以com結尾的行,則可以匹配到文件的”my email address is [email protected]”這樣,所以在該行後面增加了”hello字符串。

i 插入

同a追加,不同處是a是將字符串追擊到在匹配的行的後一行,而i則是將字符串插入到匹配的行的前一行

c 取代

取代指定單行

取代第1行

[root@localhost ~]# sed '1c hello' test.txt
hello
my email address is kwinwong@hotmail.com
my blog is http://blog.csdn.net/kwinh
Please contact me if you have any questions
end

本例命令部分中的1表示第一行,同樣的第二行寫成2,第一行到第三行寫成1,3,用2, 表示第二行到最後一行中間所有的行(包含第二行和最後一行)。

範圍匹配替換

替換1到2行

[root@localhost ~]# sed '1,2c hello everyone' test.txt
hello everyone
my blog is http://blog.csdn.net/kwinh
Please contact me if you have any questions
end

正則匹配替換

[root@localhost ~]# sed '/^my.*com$/c hello everyone' test.txt
my name is kwin
hello everyone
my blog is http://blog.csdn.net/kwinh
Please contact me if you have any questions
end

s 替換

1,2表示範圍(默認所有行1,$),本例將文件中的所有kwin替換成kwinwong,最後的g是global的意思,也就是全局替換,如果不加g,則只會替換範圍行內的每一行的第一個kwin。

[root@localhost ~]# sed '1,2s/kwin/kwinwong/g' test.txt
my name is kwinwong
my email address is kwinwongwong@hotmail.com
my blog is http://blog.csdn.net/kwinh
Please contact me if you have any questions
end

正則表達式後向引用

sed命令中使用()包裹的內容表示正則表達式的第n部分,序號從1開始計算,所以\1就是kwin

[root@localhost ~]# sed 's/\(kwin\)/\1wong/g' test.txt
my name is kwinwong
my email address is kwinwongwong@hotmail.com
my blog is http://blog.csdn.net/kwinwongh
Please contact me if you have any questions
end

d 刪除

刪除指定單行

刪除第1行

[root@localhost ~]# sed '1d hello' test.txt
my email address is kwinwong@hotmail.com
my blog is http://blog.csdn.net/kwinh
Please contact me if you have any questions
end

本例命令部分中的1表示第一行,同樣的第二行寫成2,第一行到第三行寫成1,3,用2, 表示第二行到最後一行中間所有的行(包含第二行和最後一行)。

範圍匹配刪除

刪除1到2行

[root@localhost ~]# sed '1,2d hello' test.txt
my blog is http://blog.csdn.net/kwinh
Please contact me if you have any questions
end

正則匹配刪除

[root@localhost ~]# sed '/^my.*com$/d' test.txt
my name is kwin
my blog is http://blog.csdn.net/kwinh
Please contact me if you have any questions
end

p 打印

本例在屏幕上打印第三行到最後一行的內容,p命令一般和-n選項一起使用。

範圍匹配打印

[root@localhost ~]# sed '3,$p' test.txt  -n
my blog is http://blog.csdn.net/kwinh
Please contact me if you have any questions
end

正則匹配打印

[root@localhost ~]# sed -n '/^my.*com$/p' test.txt
my email address is kwinwongwong@hotmail.com
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章