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