《LINUX實戰(zhàn):Linux基礎(chǔ)知識:sed命令》要點:
本文介紹了LINUX實戰(zhàn):Linux基礎(chǔ)知識:sed命令,希望對您有用。如果有疑問,可以聯(lián)系我們。
在之前的文章中我們介紹了文本三劍客中g(shù)rep,本次博客就另外一名劍客——sed做出詳細的描述,sed真的是一款強年夜的工具.下面讓我們來一起看一下吧!
SED的英文全稱為Stream EDitor,中文稱流編纂器.默認情況下,它會一行一行的讀取文件中的內(nèi)容,在了解其工作原理之前,首先我們得先知道一下幾個概念:
1.模式空間(pattern buffer):sed從文件中讀取行首先會放到模式空間中進行執(zhí)行和處置,定義的sed命令都是在這里執(zhí)行的,默認情況下會逐行的讀,逐行的處置,除非你做了行定界.
2.堅持空間(hold buffer):在處理完模式空間的一些行的時候,我們有可能需要一個臨時的地方去存放模式空間中的內(nèi)容,這時候就可以將模式空間中的內(nèi)容放到堅持空間了.
初始情況下,模式空間和堅持空間都是空的.
1.默認環(huán)境下,將從文件中逐行的讀取內(nèi)容至模式空間;
2.默認情況下,模式空間中的內(nèi)容在處理完成后將會打印到尺度輸出;
3.sed命令在模式空間中的都是按順序執(zhí)行的,除非指定了行定界,不然將在所有的行上面執(zhí)行;
4.修改后的行被送至尺度輸出的之后,模式空間將被清空.
?語法格局:
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-n:不輸出模式空間中的內(nèi)容至尺度輸出
#例子1 [root@localhost ~]# sed '' /etc/fstab # # /etc/fstab # Created by anaconda on Wed Sep 6 11:16:57 2017 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/cl-root / xfs defaults 0 0 UUID=0ab24855-5180-4d3e-a61d-99ca54711c2c /boot xfs defaults 0 0 /dev/mapper/cl-swap swap swap defaults 0 0 [root@localhost ~]# #例子2 [root@localhost ~]# sed -n '' /etc/fstab [root@localhost ~]#
默認情況下,sed命令會逐行的讀取文件中每一行內(nèi)容至模式空間,再執(zhí)行執(zhí)行單引號內(nèi)的命令,如例子1,單引號內(nèi)沒有任何內(nèi)容,所以不會對行內(nèi)容作任何的處理,所以sed會逐行讀取文件的內(nèi)容,然后逐行的顯示到尺度輸出,每打印一行到尺度輸出,模式空間就會被清空一次.在例子2中,加了-n的選項,會阻止輸出到尺度輸出,所以就不會顯示任何的內(nèi)容.
?-e script:指定多個敕令
[root@localhost ~]# cat poetry 1)Never give up, 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain #例子3 [root@localhost ~]# sed -e '1d' -e '10d' poetry 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 11)Know it will pass, 12)And strength you will gain
使用-e選項,可以同時對行作出多個模式匹配,d的意思是刪除.這里的例子3,文件poetry是輸入,sed讀取第一行的內(nèi)容至模式1空間,然后判斷是否是第一行,判斷成功是第一行,則刪除第一行(從模式空中清除,也不會打印至標準輸出),讀取第二行,先判斷是否是第一行,不是,再判斷是否是第十行,不是,然后打印至標準輸出,所以在標準輸出中只顯示了不包括第一行和第十行的其他行的內(nèi)容.注意:sed不會修改原文件的內(nèi)容.
-f ?/path/to/script:把sed的編纂命令放在文件中,注意每一行一個命令:
#例子4 [root@localhost ~]# cat sed_script.txt #編纂命令存放的文本文件 1d 10d [root@localhost ~]# sed -f sed_script.txt poetry 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 11)Know it will pass, 12)And strength you will gain
我們可以看到例子4中sed使用-f選項指定編纂命令所在的文件,執(zhí)行的是和例子3同樣的操作,刪除第一行和第十行,當我們得編纂命令有很多,把它放在文件也是一個不錯的做法吧!
默認情況下不指定行的定界,會讀取處理整個文件的每一行.如果指定了行,還是會讀取每一行,但是在執(zhí)行各種命令操作之后,會進行判斷是不是定界的行,如果判斷成功,則執(zhí)行操作,如刪除行,如果判斷失敗,則不做任何的處理然后打印至標準輸出,下面來看一下幾種常見的定界辦法:
(1)定界為空,對全文的每一行進行處置
[root@localhost ~]# sed '' poetry 1)Never give up, 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
[root@localhost ~]# sed '2d' poetry #當判斷為第二行的時候,會刪除第二行 1)Never give up, 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
b./pattern/:被模式匹配到的行,默認可以是基本的正在表達式
[root@localhost ~]# sed '/As/ d' poetry #刪除被模式匹配的行 1)Never give up, 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
c.$末了一行
[root@localhost ~]# sed '$ d' poetry #刪除最后一行 1)Never give up, 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass,
(3)定界規(guī)模
a.x,y:x至y行
[root@localhost ~]# sed '1,9 d' poetry #刪除了1-9行 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
b.x,+y:x行到,x行往下數(shù)y個行
[root@localhost ~]# sed '1,+3 d' poetry #刪除第一行,和第一行往下的3行,也便是一直刪除到第四行 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
c.x,/pattren/:x到被模式匹配的行
[root@localhost ~]# sed '2,/smile/ d' poetry #刪除第二行至被smile匹配的行,smile被第九行匹配 1)Never give up, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
d./pattren1/,/pattern2/:被模式1匹配的行開端到被模式2匹配的行
[root@localhost ~]# sed '/Always/,/put/ d' poetry 1)Never give up, 2)Never lose hope. 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
[root@localhost ~]# sed '1~2 d' poetry #顯示偶數(shù)行 2)Never lose hope. 4)It allows you to cope. 6)As they always do. 8)Your dreams will come true. 10)You'll live through your pain. 12)And strength you will gain [root@localhost ~]# sed '2~2 d' poetry #顯示奇數(shù)行 1)Never give up, 3)Always have faith, 5)Trying times will pass, 7)Just have patience, 9)So put on a smile, 11)Know it will pass, ?
下面我們來看一下編纂命令,如前面的d選項是刪除行的意思,那么除了d選項,還有哪些呢?
p:顯示模式空間的內(nèi)容
#例5 [root@localhost ~]# sed 'p' poetry 1)Never give up, 1)Never give up, 2)Never lose hope. 2)Never lose hope. 3)Always have faith, 3)Always have faith, 4)It allows you to cope. 4)It allows you to cope. 5)Trying times will pass, 5)Trying times will pass, 6)As they always do. 6)As they always do. 7)Just have patience, 7)Just have patience, 8)Your dreams will come true. 8)Your dreams will come true. 9)So put on a smile, 9)So put on a smile, 10)You'll live through your pain. 10)You'll live through your pain. 11)Know it will pass, 11)Know it will pass, 12)And strength you will gain 12)And strength you will gain #例6 [root@localhost ~]# sed -n 'p' poetry 1)Never give up, 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
更多詳情見請繼續(xù)閱讀下一頁的出色內(nèi)容:
_baidu_page_break_tag_在例5中,每一行都被打印了2次,原因是在模式空間的時候被打印了一次,然后處理完成之后,在尺度輸出又被打印了一次,所以自然會顯示出兩次.如果使用-n,則僅僅打印的模式空間的內(nèi)容.
a test:在行后面追加文本"test",可以使用\n實現(xiàn)多行追加
[root@localhost ~]# sed '1 a hi i am here\nhey ' poetry 1)Never give up, hi i am here hey 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
i test:在?行前面插入文本test,支持使用\n實現(xiàn)多行插入
[root@localhost ~]# sed '2i hi i am here\nhey ' poetry 1)Never give up, hi i am here hey 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
c test:把匹配到的行替換為此處指定的行
[root@localhost ~]# sed '2c hi i am here ' poetry 1)Never give up, hi i am here 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
w /path/to/somefile:保留模式空間中匹配到的內(nèi)容到指定的文件中
[root@localhost ~]# sed '/So/ w /tmp/poetry' poetry 1)Never give up, 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain [root@localhost ~]# [root@localhost ~]# cat /tmp/poetry 9)So put on a smile,
r /path/from/somefile:讀取指定文件的內(nèi)容至當前文件中被模式匹配的行后面
[root@localhost ~]# cat poetry2.txt 13)yes! I can! [root@localhost ~]# sed '12 r poetry2.txt' poetry 1)Never give up, 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain 13)yes! I can!
=:為模式匹配到的行打印行號,打印在行的上面
[root@localhost ~]# sed '/Never/ =' poetry 1 1)Never give up, 2 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
!:前提取反
[root@localhost ~]# sed '/Never/ !d' poetry #不刪除被默認匹配的行 1)Never give up, 2)Never lose hope.
s///:查找替換,分割符可以自定義,好比使用s@@@flag或者s###flag,默認會替換被匹配的第一個位置,但是可以添加替換標記:
s///g:全局替換
[root@localhost ~]# cat poetry 1)Never give up, 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain [root@localhost ~]# sed 's/\<you\>/your/g' poetry 1)Never give up, 2)Never lose hope. 3)Always have faith, 4)It allows your to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength your will gain
sed默認支持的根本的正則表達式,使用-r, --regexp-extended選項可以支持擴展的正則表達式.
練習1:刪除/boot/grub2/grub.cfg文件中所有以空白字符開首的行的行首的所有空白字符
[root@localhost ~]# sed -r 's@^[[:space:]]+@@g' /boot/grub2/grub.cfg
練習2:刪除/etc/fstab文件中所有以#開首的行的行首的#號及#后面的所有空白字符
[root@localhost ~]# sed 's/^#[[:space:]]*//g' /etc/fstab
練習3:輸出一個目錄給sed,取出其目錄,相似于dirname
[root@localhost ~]# echo "/var/log/messages" | sed -r 's@[^/]+/?$@@' /var/log/ [root@localhost ~]# [root@localhost ~]# echo "/var/log/messages/" | sed -r 's@[^/]+/?$@@' /var/log/
?sed還有一些高級的命令,會用到之前說到的堅持空間.
?
舉一些例子來看一下吧!
[root@localhost ~]# cat poetry 1)Never give up, 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
可以本身先思考一下答案,再點開:
[root@localhost ~]# sed -n 'n;p' poetry #只顯示偶數(shù)行 2)Never lose hope. 4)It allows you to cope. 6)As they always do. 8)Your dreams will come true. 10)You'll live through your pain. 12)And strength you will gain
[root@localhost ~]# sed '1!G;h;$!d' poetry #逆序顯示 12)And strength you will gain 11)Know it will pass, 10)You'll live through your pain. 9)So put on a smile, 8)Your dreams will come true. 7)Just have patience, 6)As they always do. 5)Trying times will pass, 4)It allows you to cope. 3)Always have faith, 2)Never lose hope. 1)Never give up,
[root@localhost ~]# sed '$!d' poetry #只保存最后一行,類似tail -1 12)And strength you will gain
[root@localhost ~]# sed '$!N;$!D' poetry #取出最后兩行 11)Know it will pass, 12)And strength you will gain
[root@localhost ~]# sed 'G' poetry #在每一行的后面添加一個空白行 1)Never give up, 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
這些都是一些高級的用法,在工作中用的可能比較少,因為有的結(jié)果完全可以用其他簡單的命令去實現(xiàn).
?sed最牛逼之處莫過于這個了,sed提供了一個循環(huán)和分支工來控制法式的執(zhí)行流程.
?輪回
sed循環(huán)的工作原理類似于當代編程語言中的goto語句.
界說sed的標簽:
:label
跳轉(zhuǎn)到標簽的地位,使用b命令后面跟著標簽名即可,下面我們來看一個具體的實例:
[root@localhost ~]# cat teleplay 劇名:白夜追兇 評分:9.0 劇名:秘密深林 評分:9.3 劇名:權(quán)利的游戲第七季 評分:9.3 劇名:請回答1988 評分:9.6 [root@localhost ~]# sed -n ' h;n;H;x s@\n@,@ /請回答/!b label s@^@---@ :label p' teleplay 劇名:白夜追兇,評分:9.0 劇名:秘密深林,評分:9.3 劇名:權(quán)利的游戲第七季,評分:9.3 ---劇名:請回答1988,評分:9.6
闡發(fā):
由于敕令很長,我們可以將敕令分行來寫,當然也可以寫在一行里面:
1.h;n;H;x:將第一行內(nèi)容讀取到模式空間,執(zhí)行h,將模式空間的內(nèi)容覆蓋至堅持空間,執(zhí)行n,從文件中讀取第二行覆蓋至模式空間,執(zhí)行H,將模式空間的內(nèi)容追加至堅持空間,執(zhí)行x,將堅持空間的內(nèi)容和模式空間調(diào)換;(這里得到的結(jié)果就是模式空間中存在兩行);
2.s@\n@,@,將換行符替換為逗號;這樣本來的兩行就變成了一行;
3./請答復/!b label,判斷這一行中是否有“請答復”三個字符,如果沒有則直接跳到":label",然后執(zhí)行打印"p",打印模式空間中的內(nèi)容,如果有則執(zhí)行"s@^@---@",將在行首添加字符"---".
這樣一來,只有"劇名:請答復1988,評分:9.6"被匹配,然后進行了行首的替換操作.
分支
?可以使用t來創(chuàng)立分支.使用t命令跳轉(zhuǎn)到指定的標簽,一樣我們來看一個例子:
[root@localhost ~]# sed -n ' > h;n;H;x > s/\n/,/ > :loop > /白夜追兇/s/^/-/ > /-----/!t loop > p' teleplay -----劇名:白夜追兇,評分:9.0 劇名:秘密深林,評分:9.3 劇名:權(quán)利的游戲第七季,評分:9.3 劇名:請回答1988,評分:9.6
闡發(fā):
由于命令很長,我們可以將命令分行來寫,當然也可以寫在一行里面:
1:h;n;H;x:將第一行內(nèi)容讀取到模式空間,執(zhí)行h,將模式空間的內(nèi)容覆蓋至堅持空間,執(zhí)行n,從文件中讀取第二行覆蓋至模式空間,執(zhí)行H,將模式空間的內(nèi)容追加至堅持空間,執(zhí)行x,將堅持空間的內(nèi)容和模式空間調(diào)換;(這里得到的結(jié)果就是模式空間中存在兩行);
2:s@\n@,@,將換行符替換為逗號;這樣本來的兩行就變成了一行;
3::loop:界說了一個loop標簽;
4:/白夜追兇/s/^/-/:是否匹配模式,假如匹配則在其行首添加一個"-";假如不匹配,則直接打印;
5:/-----/!t loop:是否存在5個"-",如果不存在,則跳到標簽loop處,繼續(xù)執(zhí)行第4步添加"-",直到滿意5個"-",則跳出循環(huán)打印.
最后,我們之前的所有的操作,都是沒有修改文件的自己的,可以使用 -i 選項來直接修改文件自己,慎用,建議在使用之前,先備份一下文件!
[root@localhost ~]# sed -i '1d' poetry [root@localhost ~]# [root@localhost ~]# cat poetry 2)Never lose hope. 3)Always have faith, 4)It allows you to cope. 5)Trying times will pass, 6)As they always do. 7)Just have patience, 8)Your dreams will come true. 9)So put on a smile, 10)You'll live through your pain. 11)Know it will pass, 12)And strength you will gain
總結(jié):sed的確是一個強大的文本處理工具,功能非常豐富,必要在今后的日常使用和工作中不斷的熟悉和鞏固.
本文永遠更新鏈接地址:
歡迎參與《LINUX實戰(zhàn):Linux基礎(chǔ)知識:sed命令》討論,分享您的想法,維易PHP學院為您提供專業(yè)教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/7040.html