一、sed簡(jiǎn)介
sed簡(jiǎn)稱(chēng)流編輯器,即stream editor的縮寫(xiě)。sed是一個(gè)操作、過(guò)濾和轉(zhuǎn)換文本內(nèi)容的強(qiáng)大工具。常用的功能有通過(guò)結(jié)合正則表達(dá)式對(duì)文件實(shí)現(xiàn)快速的增刪改查。最常用的查詢(xún)功能是過(guò)濾(過(guò)濾指定字符串)、取行(取出指定行)。
二、sed語(yǔ)法
(一)sed常用語(yǔ)法如下:
sed[選項(xiàng)參數(shù)]'[定位符]sed內(nèi)置命令字符'文件
或
sed[選項(xiàng)參數(shù)]"[定位符]sed內(nèi)置命令字符"文件
或通過(guò)執(zhí)行命令以管道符輸送給sed命令處理
命令 | sed [選項(xiàng)參數(shù)] '[定位符]sed內(nèi)置命令字符' 文件1、sed常用選項(xiàng)參數(shù)有:
-n:表示取消默認(rèn)的sed輸出,通常與sed內(nèi)置命令p一起使用; -i:表示直接將修改結(jié)果寫(xiě)入文件,如果不加-i,sed修改的是內(nèi)存數(shù)據(jù); -e:表示多次編輯,不需要管道符號(hào); -r:表示支持正則表達(dá)式;2、sed內(nèi)置的命令字符,主要是用于對(duì)文件進(jìn)行增刪改查等操作,常見(jiàn)內(nèi)置命令字符有:
a:表示對(duì)文本進(jìn)行追加操作,在指定行后面添加一行或多行文本; d:表示刪除匹配行; c:替換行; i:表示插入文本,在指定行前添加一行或多行文本; p:表示打印匹配行內(nèi)容,通常與-n一同使用; s/正則/替換內(nèi)容/g:表示匹配正則內(nèi)容,然后替換內(nèi)容(支持正則表達(dá)式),結(jié)尾g表示全局匹配; =:打印行號(hào); r:表示讀取文件或者導(dǎo)入文件內(nèi)容; w:表示文件另存為或者導(dǎo)出文件內(nèi)容;3、sed匹配范圍主要有:
空地址:表示全文處理; 單地址:表示指定文件某一行; /pattern/:表示被模式匹配到的每一行; 范圍區(qū)間:如12,22表示十二行到二十二行,如12,+5表示第12行向下5行; 步長(zhǎng):如1~2,表示1,3,5,7,9行;2~2,表示2,4,6,8,10行;
三、sed實(shí)例合集
以下例子針對(duì)sed語(yǔ)法進(jìn)行舉例,并且都是實(shí)際工作中常用到的劍客招式。
(一)通過(guò)定位符打印文件中指定的行:
首先準(zhǔn)備一份文件sed_test.txt進(jìn)行例子演示,該文件內(nèi)容如下所示:
we are from haodaolinux xiaoming is a good boy ! xiaohong is a girl . we all like study . I like linux we stuty python you study network they study linux liming like java jam like mysql tom like redis例子1、通過(guò)sed命令打印sed_test.txt文件第4行的內(nèi)容,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -n '4p' sed_test.txt we all like study .
例子2、通過(guò)sed命令打印sed_test.txt文件第1行到第4行的內(nèi)容,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -n '1,4p' sed_test.txt we are from haodaolinux xiaoming is a good boy ! xiaohong is a girl . we all like study .例子3、通過(guò)sed命令打印sed_test.txt文件第2行以及后面的4行的內(nèi)容,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -n '2,+4p' sed_test.txt xiaoming is a good boy ! xiaohong is a girl . we all like study . I like linux we stuty python例子4、通過(guò)sed命令打印sed_test.txt文件第1行開(kāi)始,步長(zhǎng)為2的所有內(nèi)容,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -n '1~2p' sed_test.txt we are from haodaolinux xiaohong is a girl . I like linux you study network liming like java tom like redis例子5、通過(guò)sed命令打印sed_test.txt文件以we開(kāi)頭的所有行,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -rn '/^we/p' sed_test.txt we are from haodaolinux we all like study . we stuty python例子6、通過(guò)sed命令打印sed_test.txt文件中有l(wèi)ike的所有行,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -rn '/like/p' sed_test.txt we all like study . I like linux liming like java jam like mysql tom like redis例子7、通過(guò)sed命令打印sed_test.txt文件第1行,第3行,第5行的所有行,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -n '1p;3p;5p' sed_test.txt we are from haodaolinux xiaohong is a girl . I like linux例子8、通過(guò)sed命令打印sed_test.txt文件第3行以外的所有行,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -n '3!p' sed_test.txt we are from haodaolinux xiaoming is a good boy ! we all like study . I like linux we stuty python you study network they study linux liming like java jam like mysql tom like redis
例子9、通過(guò)sed命令過(guò)濾內(nèi)存信息,命令執(zhí)行如下所示:
[root@haodaolinux1 home]# free -m | sed -n '/Mem/p' Mem: 981 479 132 7 370 342例子10、通過(guò)sed命令過(guò)濾磁盤(pán)根分區(qū)的信息,命令執(zhí)行如下所示:
[root@haodaolinux1 home]# df -h | sed -n '//$/p' /dev/sda2 38G 28G 9.9G 74% /
(二)刪除指定文件中相關(guān)內(nèi)容:
如果要對(duì)源文件進(jìn)行刪除操作,需要加上-i選項(xiàng)!??!
首先準(zhǔn)備一份文件sed_test01.txt進(jìn)行例子演示,該文件內(nèi)容如下所示:
we are from haodaolinux and we are study linux . #you do not like study ? xiaoming is a good boy ! we all like study and we are the same and we are together ! I like linux we stuty python , we like java , we like linux . you study network they study linux liming like java jam like mysql tom like redis #so we will get the python book.例子1、通過(guò)sed命令刪除sed_test01.txt文件第3行的內(nèi)容,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '3d' sed_test01.txt we are from haodaolinux and we are study linux . #you do not like study ? we all like study and we are the same and we are together ! I like linux we stuty python , we like java , we like linux . you study network they study linux liming like java jam like mysql tom like redis #so we will get the python book.例子2、通過(guò)sed命令刪除sed_test01.txt文件中以#開(kāi)頭的行,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '/^#/d' sed_test01.txt we are from haodaolinux and we are study linux . xiaoming is a good boy ! we all like study and we are the same and we are together ! I like linux we stuty python , we like java , we like linux . you study network they study linux liming like java jam like mysql tom like redis例子3、通過(guò)sed命令刪除sed_test01.txt文件中不包含like的行,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '/like/!d' sed_test01.txt #you do not like study ? we all like study and we are the same and we are together ! I like linux we stuty python , we like java , we like linux . liming like java jam like mysql tom like redis例子4、通過(guò)sed命令刪除sed_test01.txt文件中空白的行,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '/^$/d' sed_test01.txt we are from haodaolinux and we are study linux . #you do not like study ? xiaoming is a good boy ! we all like study and we are the same and we are together ! I like linux we stuty python , we like java , we like linux . you study network they study linux liming like java jam like mysql tom like redis #so we will get the python book.例子5、通過(guò)sed命令將sed_test01.txt文件中所有行替換為haodaolinux,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed 'c haodaolinux' sed_test01.txt haodaolinux haodaolinux haodaolinux haodaolinux haodaolinux haodaolinux haodaolinux haodaolinux haodaolinux haodaolinux haodaolinux haodaolinux haodaolinux例子6、通過(guò)sed命令將sed_test01.txt文件中第3行替換為haodaolinux,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '3c haodaolinux' sed_test01.txt we are from haodaolinux and we are study linux . #you do not like study ? haodaolinux we all like study and we are the same and we are together ! I like linux we stuty python , we like java , we like linux . you study network they study linux liming like java jam like mysql tom like redis #so we will get the python book.例子7、通過(guò)sed命令將sed_test01.txt文件中boy所在的行替換為boy=student,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '/boy/c boy=student' sed_test01.txt we are from haodaolinux and we are study linux . #you do not like study ? boy=student we all like study and we are the same and we are together ! I like linux we stuty python , we like java , we like linux . you study network they study linux liming like java jam like mysql tom like redis #so we will get the python book.例子8、通過(guò)sed命令將sed_test01.txt文件中每一行的第1個(gè)we替換為they,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed 's/we/they/' sed_test01.txt they are from haodaolinux and we are study linux . #you do not like study ? xiaoming is a good boy ! they all like study and we are the same and we are together ! I like linux they stuty python , we like java , we like linux . you study network they study linux liming like java jam like mysql tom like redis #so they will get the python book.例子9、通過(guò)sed命令將sed_test01.txt文件中每一行的所有we替換為they,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed 's/we/they/g' sed_test01.txt they are from haodaolinux and they are study linux . #you do not like study ? xiaoming is a good boy ! they all like study and they are the same and they are together ! I like linux they stuty python , they like java , they like linux . you study network they study linux liming like java jam like mysql tom like redis #so they will get the python book.例子10、通過(guò)sed命令將sed_test01.txt文件中每一行的第3個(gè)we替換為they,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed 's/we/they/3' sed_test01.txt we are from haodaolinux and we are study linux . #you do not like study ? xiaoming is a good boy ! we all like study and we are the same and they are together ! I like linux we stuty python , we like java , they like linux . you study network they study linux liming like java jam like mysql tom like redis #so we will get the python book.例子11、通過(guò)sed命令將sed_test01.txt文件中每一行的所有we替換為(we),命令執(zhí)行如下:
[root@haodaolinux1 home]# sed 's/we/(&)/g' sed_test01.txt (we) are from haodaolinux and (we) are study linux . #you do not like study ? xiaoming is a good boy ! (we) all like study and (we) are the same and (we) are together ! I like linux (we) stuty python , (we) like java , (we) like linux . you study network they study linux liming like java jam like mysql tom like redis #so (we) will get the python book.例子12、通過(guò)sed命令將sed_test01.txt文件中第4行的所有we替換為they,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '4s/we/they/g' sed_test01.txt we are from haodaolinux and we are study linux . #you do not like study ? xiaoming is a good boy ! they all like study and they are the same and they are together ! I like linux we stuty python , we like java , we like linux . you study network they study linux liming like java jam like mysql tom like redis #so we will get the python book.例子13、通過(guò)sed命令將sed_test01.txt文件中第4行的所有we進(jìn)行刪除,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '4s/we//g' sed_test01.txt we are from haodaolinux and we are study linux . #you do not like study ? xiaoming is a good boy ! all like study and are the same and are together ! I like linux we stuty python , we like java , we like linux . you study network they study linux liming like java jam like mysql tom like redis #so we will get the python book.例子14、通過(guò)sed命令將sed_test01.txt文件中第4行的第1個(gè)we替換為they,并且只將該行打印輸出,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -n '4s/we/they/p' sed_test01.txt they all like study and we are the same and we are together !例子15、通過(guò)sed命令將sed_test01.txt文件中第4行的行號(hào)打印輸出,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -n '4=' sed_test01.txt 4例子15、通過(guò)sed命令將sed_test01.txt文件中包含like的行的行號(hào)打印輸出,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -n '/like/=' sed_test01.txt 2 4 6 7 10 11 12例子16、通過(guò)sed命令將sed_test01.txt文件中將以java結(jié)尾的行的行號(hào)打印輸出,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -n '/java$/=' sed_test01.txt 10例子17、通過(guò)sed命令將sed_test01.txt文件中總的行號(hào)打印輸出,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -n '$=' sed_test01.txt 13
(三)通過(guò)sed對(duì)文件中的多行文本進(jìn)行處理:
如果要對(duì)源文件進(jìn)行編輯操作,需要加上-i選項(xiàng)!!!
首先準(zhǔn)備一份文件sed_test02.txt進(jìn)行例子演示,該文件內(nèi)容如下所示:
linux python network linux python linux c mysql java html php go go mysql python python php c++ python php例子1、通過(guò)sed命令在sed_test02.txt文件中的第2行前插入we are study,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '2i we are study' sed_test02.txt linux python network linux we are study python linux c mysql java html php go go mysql python python php c++ python php例子2、通過(guò)sed命令在sed_test02.txt文件中有mysql的行前插入youare study,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '/mysql/i you are study' sed_test02.txt linux python network linux you are study python linux c mysql java html php go you are study go mysql python python php c++ python php例子3、通過(guò)sed命令在sed_test02.txt文件中的第2行h后追加we are study too,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '2a we are study too' sed_test02.txt linux python network linux python linux c mysql we are study too java html php go go mysql python python php c++ python php例子4、通過(guò)sed命令在sed_test02.txt文件中有mysql的行后追加youare study too,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '/mysql/a you are study too' sed_test02.txt linux python network linux python linux c mysql you are study too java html php go go mysql python python you are study too php c++ python php例子5、兩個(gè)文件內(nèi)容合并實(shí)例: 首先創(chuàng)建read.txt文件內(nèi)容如下所示:
we are study you are study they are study(1)通過(guò)sed命令讀取read.txt文件的內(nèi)容,將讀取內(nèi)容追加到sed_test02.txt文件中每一行的后面,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed 'r read.txt' sed_test02.txt linux python network linux we are study you are study they are study python linux c mysql we are study you are study they are study java html php go we are study you are study they are study go mysql python python we are study you are study they are study php c++ python php we are study you are study they are study(2)通過(guò)sed命令讀取read.txt文件的內(nèi)容,將讀取內(nèi)容追加到sed_test02.txt文件中的第3行的后面,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '3r read.txt' sed_test02.txt linux python network linux python linux c mysql java html php go we are study you are study they are study go mysql python python php c++ python php(3)通過(guò)sed命令讀取read.txt文件的內(nèi)容,將讀取內(nèi)容追加到sed_test02.txt文件中有mysql行的后面,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed '/mysql/r read.txt' sed_test02.txt linux python network linux python linux c mysql we are study you are study they are study java html php go go mysql python python we are study you are study they are study php c++ python php例子6:文件內(nèi)容另存為其它文件實(shí)例:
(1)通過(guò)sed命令讀取sed_test02.txt文件中所有內(nèi)容,另存為write.txt文件,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed 'w write.txt' sed_test02.txt linux python network linux python linux c mysql java html php go go mysql python python php c++ python php(2)通過(guò)sed命令將sed_test02.txt文件中包含mysql的行,另存為write_mysql.txt文件,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -i '/mysql/w write_mysql.txt' sed_test02.txt(3)通過(guò)sed命令將sed_test02.txt文件中1行到3行的內(nèi)容,另存為write_13.txt文件,命令執(zhí)行如下:
[root@haodaolinux1 home]# sed -i '2,3w write_13.txt' sed_test02.txt
審核編輯:劉清
-
Linux
+關(guān)注
關(guān)注
87文章
11322瀏覽量
209857 -
磁盤(pán)
+關(guān)注
關(guān)注
1文章
379瀏覽量
25224 -
SED
+關(guān)注
關(guān)注
0文章
25瀏覽量
27094
原文標(biāo)題:sed用得少?那是你沒(méi)發(fā)現(xiàn)它這些實(shí)用技巧~
文章出處:【微信號(hào):浩道linux,微信公眾號(hào):浩道linux】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論