文本處理三劍客:sed篇

Stream EDitor, 行編輯器

Sed主要用來自動編輯一個或多個文件,簡化對文件的反復操作,編寫轉換程序等。

工作原理:

QQ截圖20160809194040.jpg

sed是一種流編輯器,如上圖所示,它一次處理一行內容,將讀入的那行內容送入模式空間,然后根據sed的編輯命令對其進行響應的操作,處理完成后sed默認會把模式空間中的內容打印至標準輸出,如果指定了-n選項,則會禁止這種默認的打印行為。sed中還有一層保持空間,用于后備的緩存區,使用其高級的編輯命令可以做出很多奇復雜而又奇妙的操作。


用法及常用選項:

    用法:

        sed[option]… 'script' inputfile…

    常用選項:

        -n:不輸出模式空間內容的自動打印
        -e: 多點編輯
        -f /PATH/TO/SCRIPT_FILE: 從指定文件中讀取編輯腳本
        -r: 支持使用擴展正則表達式
        -i: 原處編輯

    script:
        '地址命令'

            地址定界:
                (1) 不給地址:對全文進行處理
                (2) 單地址:
                    #: 指定的行
                    /pattern/:被此處模式所能夠匹配到的每一行
                (3) 地址范圍:
                    #,#
                    #,+#
                    /pat1/,/pat2/
                    #,/pat1/
                (4) ~:步進
                    1~2 奇數行
                    2~2 偶數行

        編輯命令:    

            d: 刪除模式空間匹配的行
            p: 顯示模式空間中的內容
            a \text:在行后面追加文本;支持使用\n實現多行追加
            i\text:在行前面插入文本;支持使用\n實現多行插入
            c \text:替換行為單行或多行文本
            w /path/to/somefile: 保存模式匹配的行至指定文件
            r /path/from/somefile:讀取指定文件的文本至模式空
            間中匹配到的行后
            =: 為模式空間中的行打印行號
            !:模式空間中匹配行取反處理

            s///:查找替換,支持使用其它分隔符,s@@@,s###
                替換標記:
                g: 行內全局替換
            p: 顯示替換成功的行
            w /PATH/TO/SOMEFILE:將替換成功的行保存至文件中


小練習:

1、sed‘2p’ fstab

[root@localhost ~]# cat fstab 
     1    
     2    #
     3    # /etc/fstab
     4    # Created by anaconda on Mon Jul 25 12:06:44 2016
     5    #
     6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
     7    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8    #
     9    UUID=136f7cbb-d8f6-439b-aa73-3958bd33b05f /                       xfs     defaults        0 0
    10    UUID=bf3d4b2f-4629-4fd7-8d70-a21302111564 /boot                   xfs     defaults        0 0
    11    UUID=cbf33183-93bf-4b4f-81c0-ea6ae91cd4f6 /usr                    xfs     defaults        0 0
    12    UUID=5e11b173-f7e2-4994-95b9-55cc4c41f20b swap                    swap    defaults        0 0
[root@localhost ~]# sed '2p' fstab //因為默認會將模式空間的內容打印出來,2p則是
     1                         //把第二行的打印出來,所以會出現兩次
     2    #
     2    #
     3    # /etc/fstab
     4    # Created by anaconda on Mon Jul 25 12:06:44 2016
     5    #
     6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
     7    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8    #
     9    UUID=136f7cbb-d8f6-439b-aa73-3958bd33b05f /                       xfs     defaults        0 0
    10    UUID=bf3d4b2f-4629-4fd7-8d70-a21302111564 /boot                   xfs     defaults        0 0
    11    UUID=cbf33183-93bf-4b4f-81c0-ea6ae91cd4f6 /usr                    xfs     defaults        0 0
    12    UUID=5e11b173-f7e2-4994-95b9-55cc4c41f20b swap                    swap    defaults        0 0

2、sed –n ‘2p’ fstab

[root@localhost ~]# sed -n '2p' fstab //-n將禁止默認的打印行為,只顯示第二行
     2    #

3、sed –n ‘1,4p’ fstab

[root@localhost ~]# sed -n '1,4p' fstab //1,4表示第一行到第四行,同樣-n禁止默認打印行為
     1    
     2    #
     3    # /etc/fstab
     4    # Created by anaconda on Mon Jul 25 12:06:44 2016

4、sed –n ‘/xfs/p’ fstab

[root@localhost ~]# sed -n '/xfs/p' fstab // /xfs/為地址定界,表示將含有xfs的行打印出來
     9    UUID=136f7cbb-d8f6-439b-aa73-3958bd33b05f /                       xfs     defaults        0 0
    10    UUID=bf3d4b2f-4629-4fd7-8d70-a21302111564 /boot                   xfs     defaults        0 0
    11    UUID=cbf33183-93bf-4b4f-81c0-ea6ae91cd4f6 /usr                    xfs     defaults        0 0

5、sed-n ‘/UUID/=’fstab

[root@localhost ~]# sed -n '/UUID/=' fstab //打印行號
9
10
11
12

6、sed‘/UUID/a\superman’ fstab

[root@localhost ~]# sed '/UUID/a\superman' fstab//在含有UUID的行后追加superman
     1    
     2    #
     3    # /etc/fstab
     4    # Created by anaconda on Mon Jul 25 12:06:44 2016
     5    #
     6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
     7    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8    #
     9    UUID=136f7cbb-d8f6-439b-aa73-3958bd33b05f /                       xfs     defaults        0 0
superman
    10    UUID=bf3d4b2f-4629-4fd7-8d70-a21302111564 /boot                   xfs     defaults        0 0
superman
    11    UUID=cbf33183-93bf-4b4f-81c0-ea6ae91cd4f6 /usr                    xfs     defaults        0 0
superman
    12    UUID=5e11b173-f7e2-4994-95b9-55cc4c41f20b swap                    swap    defaults        0 0
superman

7、sed‘/UUID/i\superman’ fstab

[root@localhost ~]# sed '/UUID/i\superman' fstab //在含有UUID的行前加上superman
     1    
     2    #
     3    # /etc/fstab
     4    # Created by anaconda on Mon Jul 25 12:06:44 2016
     5    #
     6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
     7    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8    #
superman
     9    UUID=136f7cbb-d8f6-439b-aa73-3958bd33b05f /                       xfs     defaults        0 0
superman
    10    UUID=bf3d4b2f-4629-4fd7-8d70-a21302111564 /boot                   xfs     defaults        0 0
superman
    11    UUID=cbf33183-93bf-4b4f-81c0-ea6ae91cd4f6 /usr                    xfs     defaults        0 0
superman
    12    UUID=5e11b173-f7e2-4994-95b9-55cc4c41f20b swap                    swap    defaults        0 0

8、sed‘/UUID/c\superman’ fstab

[root@localhost ~]# sed '/UUID/c\superman' fstab //將被//匹配到的行替換為superman
     1    
     2    #
     3    # /etc/fstab
     4    # Created by anaconda on Mon Jul 25 12:06:44 2016
     5    #
     6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
     7    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8    #
superman
superman
superman
superman

9、sed –n –e ‘/UUID/p’ –e ‘/UUID/=’fstab

[root@localhost ~]# sed  -n -e  '/UUID/P' -e '/UUID/=' fstab //因為前面的行沒有被匹配到,然后將第九行讀到模式空間,然后打印出來,-e繼續打印此行號,然后繼續讀取下一行
     9    UUID=136f7cbb-d8f6-439b-aa73-3958bd33b05f /                       xfs     defaults        0 0
9
    10    UUID=bf3d4b2f-4629-4fd7-8d70-a21302111564 /boot                   xfs     defaults        0 0
10
    11    UUID=cbf33183-93bf-4b4f-81c0-ea6ae91cd4f6 /usr                    xfs     defaults        0 0
11
    12    UUID=5e11b173-f7e2-4994-95b9-55cc4c41f20b swap                    swap    defaults        0 0
12

10、sed‘1,10d’fstab

[root@localhost ~]# sed '1,10d' fstab //刪除1-10行
    11    UUID=cbf33183-93bf-4b4f-81c0-ea6ae91cd4f6 /usr                    xfs     defaults        0 0
    12    UUID=5e11b173-f7e2-4994-95b9-55cc4c41f20b swap                    swap    defaults        0 0

11、sed 's/UUID/uuid/g'  fstab

[root@localhost ~]# sed 's/UUID/uuid/g' fstab //將匹配到的UUID替換為uuid
     1    
     2    #
     3    # /etc/fstab
     4    # Created by anaconda on Mon Jul 25 12:06:44 2016
     5    #
     6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
     7    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8    #
     9    uuid=136f7cbb-d8f6-439b-aa73-3958bd33b05f /                       xfs     defaults        0 0
    10    uuid=bf3d4b2f-4629-4fd7-8d70-a21302111564 /boot                   xfs     defaults        0 0
    11    uuid=cbf33183-93bf-4b4f-81c0-ea6ae91cd4f6 /usr                    xfs     defaults        0 0
    12    uuid=5e11b173-f7e2-4994-95b9-55cc4c41f20b swap                    swap    defaults        0 0


高級編輯命令:

    h: 把模式空間中的內容覆蓋至保持空間中
    H:把模式空間中的內容追加至保持空間中
    g: 從保持空間取出數據覆蓋至模式空間
    G:從保持空間取出內容追加至模式空間
    x: 把模式空間中的內容與保持空間中的內容進行互換
    n: 讀取匹配到的行的下一行覆蓋至模式空間
    N:追加匹配到的行的下一行至模式空間
    d: 刪除模式空間中的行
    D:刪除當前模式空間開端至\n的內容(不在傳至標準輸出),放棄之后的命令,但是對剩余模式空間重新執行sed

簡單舉例:


1、sed -n 'n;p'  fstab

[root@localhost ~]# sed -n 'n;p' fstab /
     2    #
     4    # Created by anaconda on Mon Jul 25 12:06:44 2016
     6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
     8    #
    10    UUID=bf3d4b2f-4629-4fd7-8d70-a21302111564 /boot                   xfs     defaults        0 0
    12    UUID=5e11b173-f7e2-4994-95b9-55cc4c41f20b swap                    swap    defaults        0 0

(1)首先-n禁止默認的打印功能,因為沒有地址定界,所以默認為全文,首先讀第一行至模式空間,n表示匹配到的下一行覆蓋至模式空間,即用第二行覆蓋模式空間內的第一行;

(2)然后讀取第三行,繼續-n操作,所以打印出來的都為偶數行。

2、sed '1!G;h;$!d' fstab

[root@localhost ~]# sed '1!G;h;$!d' fstab
    12    UUID=5e11b173-f7e2-4994-95b9-55cc4c41f20b swap                    swap    defaults        0 0
    11    UUID=cbf33183-93bf-4b4f-81c0-ea6ae91cd4f6 /usr                    xfs     defaults        0 0
    10    UUID=bf3d4b2f-4629-4fd7-8d70-a21302111564 /boot                   xfs     defaults        0 0
     9    UUID=136f7cbb-d8f6-439b-aa73-3958bd33b05f /                       xfs     defaults        0 0
     8    #
     7    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     6    # Accessible filesystems, by reference, are maintained under '/dev/disk'
     5    #
     4    # Created by anaconda on Mon Jul 25 12:06:44 2016
     3    # /etc/fstab
     2    #
     1

(1)依然是默認為全文,首先讀取第一行,1!G,不做操作,h,覆蓋至保持空間,因為保持空間本開始沒有內容,所以此時保持空間內容為第一行,$!d,不是最后一行,執行刪除。此第一行操作完畢,此時模式空間沒有內容,保持空間為第一行;

(2)再讀取第二行,1!G,因為不是第一行,執行G操作,即從保持空間取出內容追加至模式空間,此時模式空間的內容為第二行+第一行,且第一行在下面,h,把模式空間中的內容覆蓋至保持空間中,此時保持空間為原來的模式空間內容,$!d,不是最后一行,執行刪除。此時模式空間為空,保持空間為第二行+第一行。

(3)直到讀到最后一行,1!G,此時模式空間內為第十行到第一行,h,再把模式空間內容覆蓋至保持空間,最后$!d,因為是最后一行,所以不予操作,最后將模式空間的內容打印出來,即逆序顯示。

3、sed '$!d' fstab

[root@localhost ~]# sed '$!d' fstab 
    12    UUID=5e11b173-f7e2-4994-95b9-55cc4c41f20b swap                    swap    defaults        0 0

默認為全文內容,$!d表示不是最后一行執行d刪除操作,所以只打印出最后一行

原創文章,作者:我的滑板鞋,如若轉載,請注明出處:http://www.www58058.com/32089

(2)
我的滑板鞋我的滑板鞋
上一篇 2016-08-10
下一篇 2016-08-10

相關推薦

  • 關于文本處理工具之SED

                                                      &nbsp…

    學員作品 2016-08-10
  • vim文本處理工具

    vim編輯器     1、文本的編輯器的種類:         行編輯器:所謂的行編輯器是指一行一行來編輯處理的工具,如sed         全屏編輯器:編輯空間占據整個屏幕,如…

    學員作品 2016-08-10
  • Linux進程查看和管理及作業控制

    在linux系統中,內核的功用有:進程管理、文件系統、網絡功能、內存管理、驅動程序、安全功能等,在這眾多的模塊中,進程管理是相對重要的一環,即使不像文件系統和網絡功能那么復雜。在進程管理中,內核對進程的創建、切換、撤銷和調度都有很詳細的定義。  1、進程類型     守護進程:在系統引導過程中啟動的進程,跟終端無關的進…

    學員作品 2016-11-14
  • 第六次作業

    1 、取本機ip地址 Centos6.8 ifconfig | head -2|tail -1|cut -d: -f2|cut -d" " -f1 ifconfig | head -2|tail -1|cut&…

    學員作品 2016-08-10
  • 91-ansible

    一. Ansible      Configuration、Command and Control

    2016-11-18
  • 這個寒冬,如何趕走職場之“霾”

    在父母眼中,我的工作應該是這樣的。。。 在朋友眼中,我的工作應該是這樣的。。。 在女朋友眼中,我的工作應該是這樣的。。。 其實我是這樣的。。。 每當夜深人靜的時候,我總會站在陽臺上,獨自一個人陷入沉思,究竟是誰走漏了風聲,搞的大家都知道我窮(搬磚的)。。。。 面對每個月干癟癟的錢包和房東突然間給的驚喜房租要漲,有種想跳槽的沖動,但是互聯網寒冬被大家傳的神乎其…

    2015-12-10
欧美性久久久久