• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            learn sed 參數

            Posted on 2009-09-02 15:14 T.S Liu 閱讀(400) 評論(0)  編輯 收藏 引用 所屬分類: ubuntu

            Linux sed 15個參數


            2008-07-17 23:00:57
            版權聲明:原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://future.blog.51cto.com/26959/88400
            一、15個參數
            1、r 從文件讀入
            [root@watchout2 ~]# cat file
            1
            2
            3
            4
            5
            [root@watchout2 ~]# cat newfile
            a
            b
            c
            d
            e
            [root@watchout2 ~]# sed '/a/r file' newfile (讀入文件file,并顯示在newfile文件中匹配行a之后)
            a
            1
            2
            3
            4
            5
            b
            c
            d
            e
            [root@watchout2 ~]# touch /file
            [root@watchout2 ~]# echo "aaaaaaaaaaaaaaaaaaaaaaaaaa" > /file
            [root@watchout2 ~]# sed '/a/r /file' newfile  (讀入的文件在不同的路徑)
            a
            aaaaaaaaaaaaaaaaaaaaaaaaaa
            b
            c
            d
            e
            [root@watchout2 ~]#
             
            2、w寫入文件
            [root@watchout2 ~]# sed '/a/w bo' newfile
            a
            b
            c
            d
            e
            [root@watchout2 ~]# cat bo ([root@watchout2 ~]# sed -n '/a/w bobo' newfile )
            a
             
            3、a 追加命令
            [root@watchout2 ~]# sed '/b/a \bobo' newfile
            a
            b
            bobo
            c
            d
            e
             
            4、i 插入
            [root@watchout2 ~]# sed '/a/i \bobo' newfile
            bobo
            a
            b
            c
            d
            e
             
            5、n 下一個
            [root@watchout2 ~]# sed -n '/a/{n;p}' newfile(打印匹配行的下一行)
            b
             
            [root@watchout2 ~]# sed '/a/{n;s/b/c/p}' newfile
            a
            c
            c
            c
            d
            e
             
            6、y 變形命令
            [root@watchout2 ~]# sed '1,3y/abc/ABC/' newfile
            A
            B
            C
            d
            e
            y命令就是將小寫轉換成了大寫,正則表達式元字符不能使用這個命令。
             
            7、q 退出命令
            [root@watchout2 ~]# sed '3q' newfile
            a
            b
            c
            打印前三行后退出。
             
            8、h命令   是將pattern space 模式空間(臨時緩沖區)的內容復制到holding buffer保持緩沖區
            9、G命令 是將holding buffer中的內容取得,爾后放回pattern space中,且追加到相應行的末 尾 
                g命令是將holding buffer 中的內容取得,爾后放回pattern space 中,且替換相應的行
             
            [root@watchout2 ~]# sed -e '/a/h' -e '/d/G' newfile
            a
            b
            c
            d
            a
            e
            h命令會把a匹配行,放入保持緩沖區,G命令會把保持緩沖區中的內容放入模式空間,并追加到匹配行d的下一行。
             
            [root@watchout2 ~]# sed -e '/a/h' -e '$G' newfile
            a
            b
            c
            d
            e
            a
            與上相同,只是$代表最后一行。
             
            [root@watchout2 ~]# sed -e '/a/h' -e '/b/g' newfile
            a
            a
            c
            d
            e
            [root@watchout2 ~]# sed -e '/a/h' -e '$g' newfile
            a
            b
            c
            d
            a
             
            以上h命令會把匹配行a,放入保持緩沖區,g命令會讀取保持緩沖區的內容,將匹配行b(第二個例子就是$最后一行)替換為a 。注:a將覆蓋匹配行b(第二個例子就是$最后一行)
             
            10、x命令 是pattern space模式空間將被holding buffer保持緩沖區中的內容替換
            [root@watchout2 ~]# sed -e '/a/h' -e '/d/x' newfile
            a
            b
            c
            a
            e
            匹配行d 將被匹配行a替換。
             
            11、-n選項取消sed的默認行為,sed 默認行為是-p ,
            root:/tmp>sed '/6/p' num   -p參數打印num的內容,爾后匹配“6”在次打印6,所以6會出現兩次。
            1
            2
            3
            4
            5
            6
            6
            7
            8
            root:/tmp>sed -n '/6/p' num  -n選項取消sed的默認行為(-p ),所以只打印“6”
            6
            root:/tmp>
             
            12、刪除:d命令
            刪除第6行
            root:/tmp>sed '6d' num
            1
            2
            3
            4
            5
            7
            8
            root:/tmp>
             
            從第6行刪除到行尾
            root:/tmp>sed '6,$d' num
            1
            2
            3
            4
            5
            root:/tmp>
             
            d刪除最后一行
            root:/tmp>sed '$d' num
            1
            2
            3
            4
            5
            6
            7
            root:/tmp>
             
            d刪除匹配行
            root:/tmp>sed '/6/d' num
            1
            2
            3
            4
            5
            7
            8
            root:/tmp>
             
            13、替換:s命令
            s表示替換,g表示作用范圍整個行,如果沒有g標志則只有每行第一個被替換
            root:/tmp>sed 's/1/8/g' num    (sed默認有-p參數打印搜索后的所有行。如g后面加p,那么匹配行就會打印兩次)
            8
            2
            3
            4
            5
            6
            7
            8
            root:/tmp>
             
            行首“1”開頭被替換為“8”
            root:/tmp>sed 's/^1/8/g' num
            8
            2
            3
            4
            5
            6
            7
            8
            root:/tmp>
             
            &符號表示替換字符串中被找到的部分,所以每個數字后面被追加.6789
            root:/tmp>sed 's/[0-9]/&.6789/g' num
            1.6789
            2.6789
            3.6789
            4.6789
            5.6789
            6.6789
            7.6789
            8.6789
            root:/tmp>
             
            \(..\) 保存匹配的字符到標簽\1中。  s/\(5\)/\1.6789/  藍色部分保存到標簽\1中。從表達式最左邊開始,向右最多可以使用9個標簽
            root:/tmp>sed 's/\(5\)/\1.6789/g' num
            1
            2
            3
            4
            5.6789
            6
            7
            8
            root:/tmp>sed 's/\([0-9]\)/\1.6789/g' num
            1.6789
            2.6789
            3.6789
            4.6789
            5.6789
            6.6789
            7.6789
            8.6789
            root:/tmp>
             
            s后面的字符是分隔搜索字符串和替換字符串的分隔符。默認分隔符是斜杠,不論什么字符緊跟s命令都被認為是新的分隔符
            root:/tmp>sed 's#6#shell#g' num
            1
            2
            3
            4
            5
            shell
            7
            8
            root:/tmp>
             
            14、多點編輯:e命令
            root:/tmp>sed -e '1,6d' -e 's/8/8.shell/g' num
            7
            8.shell
            root:/tmp>
             
            15、-f  引導sed腳本文件名
             
             
            16、選定行的范圍:逗號
             
            從第一行到第五行
            root:/tmp>sed -n '1,5p' num
            1
            2
            3
            4
            5
             
            從第一行到第五行,然后在搜索
            root:/tmp>sed '1,5s/3/3.linux/g' num
            1
            2
            3.linux
            4
            5
            6
            7
            8
             
            顯示從第三行到行首為6的中間所有行
            root:/tmp>sed -n '3,/^6/p' num
            3
            4
            5
            6
            root:/tmp>
             
             
            匹配1和5后面追加“******Total********”
            root:/tmp>sed '/1/,/5/s/$/******Total********/g' num
            1******Total********
            2******Total********
            3******Total********
            4******Total********
            5******Total********
            6
            7
            8
            root:/tmp>
            亚洲综合精品香蕉久久网97| 色欲综合久久躁天天躁蜜桃| 久久精品成人免费国产片小草| 青春久久| 精品国产VA久久久久久久冰| 久久99精品国产麻豆不卡| 久久久久se色偷偷亚洲精品av| 99久久久国产精品免费无卡顿| 久久国产精品一区| 久久国产乱子伦免费精品| 久久久久一级精品亚洲国产成人综合AV区 | 色妞色综合久久夜夜| 国产成人香蕉久久久久| 色欲综合久久躁天天躁蜜桃| 理论片午午伦夜理片久久| 久久精品黄AA片一区二区三区| 性做久久久久久久久浪潮| 91精品久久久久久无码| AAA级久久久精品无码片| 午夜人妻久久久久久久久| 四虎国产精品免费久久| 久久e热在这里只有国产中文精品99 | 成人午夜精品无码区久久| 四虎影视久久久免费| 久久久久国产一区二区三区| 大美女久久久久久j久久| 色综合久久综精品| 久久国产精品成人免费| 国产三级久久久精品麻豆三级| 色婷婷综合久久久久中文一区二区| 亚洲精品国精品久久99热| 久久这里的只有是精品23| 亚洲欧美一级久久精品| 少妇熟女久久综合网色欲| 一个色综合久久| 亚洲乱码中文字幕久久孕妇黑人 | 国产精品久久久久久久人人看 | 久久精品成人影院| 亚洲中文字幕伊人久久无码| 99久久做夜夜爱天天做精品| 99精品国产99久久久久久97|