濮阳杆衣贸易有限公司

主頁 > 知識庫 > linux sed命令詳解

linux sed命令詳解

熱門標(biāo)簽:商店地圖標(biāo)注外賣入駐 磁力導(dǎo)航地圖標(biāo)注 外呼系統(tǒng)怎么弄 地圖標(biāo)注的牌子 外呼系統(tǒng)鏈接 桂林市ai電銷機(jī)器人公司 制作地圖標(biāo)注 地址高德地圖標(biāo)注 新科火車站地圖標(biāo)注點(diǎn)

sed 是一種在線編輯器,它一次處理一行內(nèi)容。處理時,把當(dāng)前處理的行存儲在臨時緩沖區(qū)中,稱為“模式空間”(pattern space),接著用sed命令處理緩沖區(qū)中的內(nèi)容,處理完成后,把緩沖區(qū)的內(nèi)容送往屏幕。接著處理下一行,這樣不斷重復(fù),直到文件末尾。文件內(nèi)容并沒有 改變,除非你使用重定向存儲輸出。Sed主要用來自動編輯一個或多個文件;簡化對文件的反復(fù)操作;編寫轉(zhuǎn)換程序等。

sed使用參數(shù)


復(fù)制代碼
代碼如下:

[root@www ~]# sed [-nefr] [動作]
選項(xiàng)與參數(shù):
-n :使用安靜(silent)模式。在一般 sed 的用法中,所有來自 STDIN 的數(shù)據(jù)一般都會被列出到終端上。但如果加上 -n 參數(shù)后,則只有經(jīng)過sed 特殊處理的那一行(或者動作)才會被列出來。
-e :直接在命令列模式上進(jìn)行 sed 的動作編輯;
-f :直接將 sed 的動作寫在一個文件內(nèi), -f filename 則可以運(yùn)行 filename 內(nèi)的 sed 動作;
-r :sed 的動作支持的是延伸型正規(guī)表示法的語法。(默認(rèn)是基礎(chǔ)正規(guī)表示法語法)
-i :直接修改讀取的文件內(nèi)容,而不是輸出到終端。/p> p>動作說明: [n1[,n2]]function
n1, n2 :不見得會存在,一般代表『選擇進(jìn)行動作的行數(shù)』,舉例來說,如果我的動作是需要在 10 到 20 行之間進(jìn)行的,則『 10,20[動作行為] 』/p> p>function:
a :新增, a 的后面可以接字串,而這些字串會在新的一行出現(xiàn)(目前的下一行)~
c :取代, c 的后面可以接字串,這些字串可以取代 n1,n2 之間的行!
d :刪除,因?yàn)槭莿h除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而這些字串會在新的一行出現(xiàn)(目前的上一行);
p :列印,亦即將某個選擇的數(shù)據(jù)印出。通常 p 會與參數(shù) sed -n 一起運(yùn)行~
s :取代,可以直接進(jìn)行取代的工作哩!通常這個 s 的動作可以搭配正規(guī)表示法!例如 1,20s/old/new/g 就是啦!

以行為單位的新增/刪除

將 /etc/passwd 的內(nèi)容列出并且列印行號,同時,請將第 2~5 行刪除!


復(fù)制代碼
代碼如下:

[root@www ~]# nl /etc/passwd | sed '2,5d'
1 root:x:0:0:root:/root:/bin/bash
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
.....(后面省略).....

sed 的動作為 '2,5d' ,那個 d 就是刪除!因?yàn)?2-5 行給他刪除了,所以顯示的數(shù)據(jù)就沒有 2-5 行羅~ 另外,注意一下,原本應(yīng)該是要下達(dá) sed -e 才對,沒有 -e 也行啦!同時也要注意的是, sed 后面接的動作,請務(wù)必以 '' 兩個單引號括住喔!

只要刪除第 2 行


復(fù)制代碼
代碼如下:

nl /etc/passwd | sed '2d'

要刪除第 3 到最后一行


復(fù)制代碼
代碼如下:

nl /etc/passwd | sed '3,$d'

在第二行后(亦即是加在第三行)加上『drink tea?』字樣!


復(fù)制代碼
代碼如下:

[root@www ~]# nl /etc/passwd | sed '2a drink tea'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
drink tea
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
.....(后面省略).....

那如果是要在第二行前


復(fù)制代碼
代碼如下:

nl /etc/passwd | sed '2i drink tea'

如果是要增加兩行以上,在第二行后面加入兩行字,例如『Drink tea or .....』與『drink beer?』


復(fù)制代碼
代碼如下:

[root@www ~]# nl /etc/passwd | sed '2a Drink tea or ......\
> drink beer ?'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
Drink tea or ......
drink beer ?
daemon:x:2:2:daemon:/sbin:/sbin/nologin
.....(后面省略).....

每一行之間都必須要以反斜杠『 \ 』來進(jìn)行新行的添加喔!所以,上面的例子中,我們可以發(fā)現(xiàn)在第一行的最后面就有 \ 存在。

以行為單位的替換與顯示

將第2-5行的內(nèi)容取代成為『No 2-5 number』呢?


復(fù)制代碼
代碼如下:

[root@www ~]# nl /etc/passwd | sed '2,5c No 2-5 number'
1 root:x:0:0:root:/root:/bin/bash
No 2-5 number
6 sync:x:5:0:sync:/sbin:/bin/sync
.....(后面省略).....

透過這個方法我們就能夠?qū)?shù)據(jù)整行取代了!

僅列出 /etc/passwd 文件內(nèi)的第 5-7 行


復(fù)制代碼
代碼如下:

[root@www ~]# nl /etc/passwd | sed -n '5,7p'
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

可以透過這個 sed 的以行為單位的顯示功能, 就能夠?qū)⒛骋粋€文件內(nèi)的某些行號選擇出來顯示。
 
數(shù)據(jù)的搜尋并顯示
搜索 /etc/passwd有root關(guān)鍵字的行


復(fù)制代碼
代碼如下:

nl /etc/passwd | sed '/root/p'
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
....下面忽略

如果root找到,除了輸出所有行,還會輸出匹配行。

 
使用-n的時候?qū)⒅淮蛴“0宓男小?/p>


復(fù)制代碼
代碼如下:

nl /etc/passwd | sed -n '/root/p'
1 root:x:0:0:root:/root:/bin/bash

數(shù)據(jù)的搜尋并刪除
刪除/etc/passwd所有包含root的行,其他行輸出


復(fù)制代碼
代碼如下:

nl /etc/passwd | sed '/root/d'
2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh
3 bin:x:2:2:bin:/bin:/bin/sh
....下面忽略
#第一行的匹配root已經(jīng)刪除了

數(shù)據(jù)的搜尋并執(zhí)行命令
找到匹配模式eastern的行后,

搜索/etc/passwd,找到root對應(yīng)的行,執(zhí)行后面花括號中的一組命令,每個命令之間用分號分隔,這里把bash替換為blueshell,再輸出這行:


復(fù)制代碼
代碼如下:

nl /etc/passwd | sed -n '/root/{s/bash/blueshell/;p}' 1 root:x:0:0:root:/root:/bin/blueshell

如果只替換/etc/passwd的第一個bash關(guān)鍵字為blueshell,就退出


復(fù)制代碼
代碼如下:

nl /etc/passwd | sed -n '/bash/{s/bash/blueshell/;p;q}'
1 root:x:0:0:root:/root:/bin/blueshell

最后的q是退出。

數(shù)據(jù)的搜尋并替換

除了整行的處理模式之外, sed 還可以用行為單位進(jìn)行部分?jǐn)?shù)據(jù)的搜尋并取代。基本上 sed 的搜尋與替代的與 vi 相當(dāng)?shù)念愃?!他有點(diǎn)像這樣:


復(fù)制代碼
代碼如下:

sed 's/要被取代的字串/新的字串/g'

先觀察原始信息,利用 /sbin/ifconfig 查詢 IP


復(fù)制代碼
代碼如下:

[root@www ~]# /sbin/ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:90:CC:A6:34:84
inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::290:ccff:fea6:3484/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
.....(以下省略).....

本機(jī)的ip是192.168.1.100。

將 IP 前面的部分予以刪除


復(fù)制代碼
代碼如下:

[root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g'
192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

接下來則是刪除后續(xù)的部分,亦即: 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

將 IP 后面的部分予以刪除


復(fù)制代碼
代碼如下:

[root@www ~]# /sbin/ifconfig eth0 | grep 'inet addr' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'
192.168.1.100

多點(diǎn)編輯
一條sed命令,刪除/etc/passwd第三行到末尾的數(shù)據(jù),并把bash替換為blueshell


復(fù)制代碼
代碼如下:

nl /etc/passwd | sed -e '3,$d' -e 's/bash/blueshell/'
1 root:x:0:0:root:/root:/bin/blueshell
2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh

-e表示多點(diǎn)編輯,第一個編輯命令刪除/etc/passwd第三行到末尾的數(shù)據(jù),第二條命令搜索bash替換為blueshell。

直接修改文件內(nèi)容(危險(xiǎn)動作)

sed 可以直接修改文件的內(nèi)容,不必使用管道命令或數(shù)據(jù)流重導(dǎo)向! 不過,由於這個動作會直接修改到原始的文件,所以請你千萬不要隨便拿系統(tǒng)配置來測試! 我們還是使用下載的 regular_express.txt 文件來測試看看吧!

利用 sed 將 regular_express.txt 內(nèi)每一行結(jié)尾若為 . 則換成 !


復(fù)制代碼
代碼如下:

[root@www ~]# sed -i 's/\.$/\!/g' regular_express.txt

利用 sed 直接在 regular_express.txt 最后一行加入『# This is a test』


復(fù)制代碼
代碼如下:

[root@www ~]# sed -i '$a # This is a test' regular_express.txt

由於 $ 代表的是最后一行,而 a 的動作是新增,因此該文件最后新增『# This is a test』!

sed 的『 -i 』選項(xiàng)可以直接修改文件內(nèi)容,這功能非常有幫助!舉例來說,如果你有一個 100 萬行的文件,你要在第 100 行加某些文字,此時使用 vim 可能會瘋掉!因?yàn)槲募罅?!那怎辦?就利用 sed 啊!透過 sed 直接修改/取代的功能,你甚至不需要使用 vim 去修訂!

流編輯器sed:

sed一次處理一行文件并把輸出送往屏幕。sed把當(dāng)前處理的行存儲在臨時緩沖區(qū)中,稱為模式空間(pattern space)。一旦sed完成對模式空間中的行的處理,模式空間中的行就被送往屏幕。行被處理完成之后,就被移出模式空間,程序接著讀入下一行,處理,顯示,移出......文件輸入的最后一行被處理完以后sed結(jié)束。通過存儲每一行在臨時緩沖區(qū),然后在緩沖區(qū)中操作該行,保證了原始文件不會被破壞。

1. sed的命令和選項(xiàng):

命令 功能描述
a\  在當(dāng)前行的后面加入一行或者文本。
c\  用新的文本改變或者替代本行的文本。
d  從pattern space位置刪除行。
i\  在當(dāng)前行的上面插入文本。
h  拷貝pattern space的內(nèi)容到holding buffer(特殊緩沖區(qū))。
H  追加pattern space的內(nèi)容到holding buffer。
g  獲得holding buffer中的內(nèi)容,并替代當(dāng)前pattern space中的文本。
G  獲得holding buffer中的內(nèi)容,并追加到當(dāng)前pattern space的后面。
n  讀取下一個輸入行,用下一個命令處理新的行而不是用第一個命令。
p  打印pattern space中的行。
P  打印pattern space中的第一行。
q  退出sed。
w file  寫并追加pattern space到file的末尾。
!  表示后面的命令對所有沒有被選定的行發(fā)生作用。
s/re/string  用string替換正則表達(dá)式re。
=  打印當(dāng)前行號碼。
替換標(biāo)記  
g  行內(nèi)全面替換,如果沒有g(shù),只替換第一個匹配。
p  打印行。
x  互換pattern space和holding buffer中的文本。
y  把一個字符翻譯為另一個字符(但是不能用于正則表達(dá)式)。
選項(xiàng)  
-e  允許多點(diǎn)編輯。
-n  取消默認(rèn)輸出。

需要說明的是,sed中的正則和grep的基本相同,完全可以參照本系列的第一篇中的詳細(xì)說明。

   2.  sed實(shí)例:
    /> cat testfile
    northwest       NW     Charles Main           3.0      .98      3       34
    western          WE      Sharon Gray           5.3      .97     5       23
    southwest       SW     Lewis Dalsass          2.7      .8      2       18
    southern         SO      Suan Chin               5.1     .95     4       15
    southeast       SE       Patricia Hemenway   4.0      .7      4       17
    eastern           EA      TB Savage               4.4     .84     5       20
    northeast        NE      AM Main Jr.              5.1     .94     3       13
    north              NO      Margot Weber         4.5     .89     5       9
    central            CT      Ann Stephens          5.7     .94     5       13

    /> sed '/north/p' testfile #如果模板north被找到,sed除了打印所有行之外,還有打印匹配行。
    northwest       NW      Charles Main           3.0     .98     3       34
    northwest       NW      Charles Main           3.0     .98     3       34
    western          WE      Sharon Gray           5.3     .97     5       23
    southwest      SW      Lewis Dalsass          2.7     .8       2       18
    southern        SO       Suan Chin               5.1     .95     4       15
    southeast       SE       Patricia Hemenway   4.0     .7       4       17
    eastern           EA      TB Savage               4.4     .84     5       20
    northeast        NE      AM Main Jr.              5.1     .94     3       13
    northeast        NE      AM Main Jr.              5.1     .94     3       13
    north              NO      Margot Weber         4.5     .89     5       9
    north              NO      Margot Weber         4.5     .89     5       9
    central            CT      Ann Stephens          5.7     .94     5       13

    #-n選項(xiàng)取消了sed的默認(rèn)行為。在沒有-n的時候,包含模板的行被打印兩次,但是在使用-n的時候?qū)⒅淮蛴“0宓男小?br />    /> sed -n '/north/p' testfile
    northwest       NW      Charles Main    3.0     .98     3       34
    northeast        NE      AM Main Jr.       5.1     .94     3       13
    north              NO      Margot Weber  4.5     .89     5       9

    /> sed '3d' testfile  #第三行被刪除,其他行默認(rèn)輸出到屏幕。
    northwest       NW     Charles Main            3.0     .98     3       34
    western          WE      Sharon Gray           5.3     .97     5       23
    southern         SO      Suan Chin               5.1     .95     4       15
    southeast       SE       Patricia Hemenway   4.0     .7       4       17
    eastern           EA      TB Savage               4.4     .84     5       20
    northeast       NE       AM Main Jr.              5.1     .94     3       13
    north             NO       Margot Weber         4.5     .89     5       9
    central           CT       Ann Stephens          5.7     .94     5       13

    /> sed '3,$d' testfile  #從第三行刪除到最后一行,其他行被打印。$表示最后一行。
    northwest       NW      Charles Main    3.0     .98     3       34
    western          WE      Sharon Gray    5.3     .97     5       23

    /> sed '$d' testfile    #刪除最后一行,其他行打印。
    northwest       NW     Charles Main           3.0     .98     3       34
    western          WE     Sharon Gray           5.3     .97     5       23
    southwest       SW    Lewis Dalsass          2.7     .8      2       18
    southern         SO     Suan Chin              5.1     .95     4       15
    southeast       SE      Patricia Hemenway   4.0     .7      4       17
    eastern           EA      TB Savage             4.4     .84     5       20
    northeast       NE      AM Main Jr.             5.1     .94     3       13
    north             NO      Margot Weber        4.5     .89     5       9

    /> sed '/north/d' testfile #刪除所有包含north的行,其他行打印。
    western           WE      Sharon Gray           5.3     .97     5       23
    southwest       SW      Lewis Dalsass          2.7     .8      2       18
    southern          SO      Suan Chin               5.1     .95     4       15
    southeast         SE      Patricia Hemenway   4.0     .7       4       17
    eastern            EA      TB Savage               4.4     .84     5       20
    central             CT      Ann Stephens          5.7     .94     5       13

    #s表示替換,g表示命令作用于整個當(dāng)前行。如果該行存在多個west,都將被替換為north,如果沒有g(shù),則只是替換第一個匹配。
    /> sed 's/west/north/g' testfile
    northnorth      NW     Charles Main           3.0     .98    3       34
    northern         WE      Sharon Gray          5.3     .97    5       23
    southnorth      SW     Lewis Dalsass         2.7     .8      2       18
    southern         SO      Suan Chin              5.1     .95    4       15
    southeast       SE      Patricia Hemenway   4.0     .7      4       17
    eastern           EA      TB Savage             4.4     .84     5       20
    northeast       NE      AM Main Jr.              5.1     .94    3       13
    north             NO      Margot Weber        4.5     .89     5       9
    central            CT      Ann Stephens        5.7     .94     5       13

    /> sed -n 's/^west/north/p' testfile #-n表示只打印匹配行,如果某一行的開頭是west,則替換為north。
    northern        WE      Sharon Gray     5.3     .97     5       23

    #符號表示替換字符串中被找到的部分。所有以兩個數(shù)字結(jié)束的行,最后的數(shù)字都將被它們自己替換,同時追加.5。
    /> sed 's/[0-9][0-9]$/.5/' testfile
    northwest       NW      Charles Main          3.0     .98     3       34.5
    western          WE      Sharon Gray           5.3     .97     5       23.5
    southwest       SW      Lewis Dalsass        2.7     .8       2       18.5
    southern         SO      Suan Chin              5.1     .95     4       15.5
    southeast       SE      Patricia Hemenway   4.0     .7       4       17.5
    eastern           EA      TB Savage              4.4     .84     5       20.5
    northeast        NE      AM Main Jr.             5.1     .94     3       13.5
    north              NO      Margot Weber        4.5     .89     5       9
    central            CT      Ann Stephens         5.7     .94     5       13.5

    /> sed -n 's/Hemenway/Jones/gp' testfile  #所有的Hemenway被替換為Jones。-n選項(xiàng)加p命令則表示只打印匹配行。
    southeast       SE      Patricia Jones  4.0     .7      4       17

    #模板Mar被包含在一對括號中,并在特殊的寄存器中保存為tag 1,它將在后面作為\1替換字符串,Margot被替換為Marlianne。
    /> sed -n 's/\(Mar\)got/\1lianne/p' testfile
    north           NO      Marlianne Weber 4.5     .89     5       9

    #s后面的字符一定是分隔搜索字符串和替換字符串的分隔符,默認(rèn)為斜杠,但是在s命令使用的情況下可以改變。不論什么字符緊跟著s命令都認(rèn)為是新的分隔符。這個技術(shù)在搜索含斜杠的模板時非常有用,例如搜索時間和路徑的時候。
    /> sed 's#3#88#g' testfile
    northwest       NW      Charles Main            88.0    .98     88     884
    western          WE       Sharon Gray           5.88    .97     5       288
    southwest       SW      Lewis Dalsass          2.7     .8       2       18
    southern         SO       Suan Chin               5.1     .95     4       15
    southeast       SE        Patricia Hemenway   4.0     .7        4       17
    eastern           EA       TB Savage               4.4     .84      5      20
    northeast        NE       AM Main Jr.              5.1     .94      88     188
    north              NO       Margot Weber         4.5     .89      5       9
    central            CT       Ann Stephens          5.7     .94      5       188

    #所有在模板west和east所確定的范圍內(nèi)的行都被打印,如果west出現(xiàn)在esst后面的行中,從west開始到下一個east,無論這個east出現(xiàn)在哪里,二者之間的行都被打印,即使從west開始到文件的末尾還沒有出現(xiàn)east,那么從west到末尾的所有行都將打印。
    /> sed -n '/west/,/east/p' testfile
    northwest       NW      Charles Main           3.0     .98      3      34
    western          WE      Sharon Gray            5.3     .97     5      23
    southwest       SW     Lewis Dalsass          2.7     .8       2      18
    southern         SO      Suan Chin               5.1     .95     4      15
    southeast        SE      Patricia Hemenway    4.0     .7       4      17

    /> sed -n '5,/^northeast/p' testfile  #打印從第五行開始到第一個以northeast開頭的行之間的所有行。
    southeast       SE      Patricia Hemenway   4.0     .7       4       17
    eastern           EA      TB Savage              4.4     .84     5       20
    northeast        NE      AM Main Jr.             5.1     .94     3       13

    #-e選項(xiàng)表示多點(diǎn)編輯。第一個編輯命令是刪除第一到第三行。第二個編輯命令是用Jones替換Hemenway。
    /> sed -e '1,3d' -e 's/Hemenway/Jones/' testfile
    southern        SO      Suan Chin          5.1     .95     4       15
    southeast       SE      Patricia Jones      4.0     .7      4       17
    eastern          EA      TB Savage          4.4     .84     5       20
    northeast       NE      AM Main Jr.         5.1     .94     3       13
    north             NO      Margot Weber    4.5     .89     5       9
    central           CT      Ann Stephens     5.7     .94     5       13

    /> sed -n '/north/w newfile' testfile #將所有匹配含有north的行寫入newfile中。
    /> cat newfile
    northwest       NW      Charles Main     3.0     .98     3       34
    northeast       NE      AM Main Jr.         5.1     .94     3       13
    north             NO      Margot Weber    4.5     .89     5       9

    /> sed '/eastern/i\ NEW ENGLAND REGION' testfile #i是插入命令,在匹配模式行前插入文本。
    northwest       NW      Charles Main          3.0     .98      3       34
    western          WE      Sharon Gray           5.3     .97     5       23
    southwest       SW      Lewis Dalsass         2.7     .8      2       18
    southern         SO      Suan Chin              5.1     .95     4       15
    southeast        SE      Patricia Hemenway   4.0     .7      4       17
    NEW ENGLAND REGION
    eastern          EA      TB Savage              4.4     .84     5       20
    northeast       NE      AM Main Jr.             5.1     .94     3       13
    north             NO      Margot Weber        4.5     .89     5       9
    central           CT      Ann Stephens         5.7     .94     5       13

    #找到匹配模式eastern的行后,執(zhí)行后面花括號中的一組命令,每個命令之間用逗號分隔,n表示定位到匹配行的下一行,s/AM/Archie/完成Archie到AM的替換,p和-n選項(xiàng)的合用,則只是打印作用到的行。
    /> sed -n '/eastern/{n;s/AM/Archie/;p}' testfile
    northeast       NE      Archie Main Jr. 5.1     .94     3       13

    #-e表示多點(diǎn)編輯,第一個編輯命令y將前三行中的所有小寫字母替換為大寫字母,-n表示不顯示替換后的輸出,第二個編輯命令將只是打印輸出轉(zhuǎn)換后的前三行。注意y不能用于正則。
    /> sed -n -e '1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' -e '1,3p' testfile
    NORTHWEST       NW      CHARLES MAIN     3.0     .98     3       34
    WESTERN           WE      SHARON GRAY      5.3     .97     5       23
    SOUTHWEST       SW      LEWIS DALSASS   2.7     .8      2       18

    /> sed '2q' testfile  #打印完第二行后退出。
    northwest       NW      Charles Main    3.0     .98     3       34
    western          WE      Sharon Gray     5.3     .97     5       23

    #當(dāng)模板Lewis在某一行被匹配,替換命令首先將Lewis替換為Joseph,然后再用q退出sed。
     /> sed '/Lewis/{s/Lewis/Joseph/;q;}' testfile
    northwest       NW      Charles Main      3.0     .98     3       34
    western          WE      Sharon Gray      5.3     .97     5       23
    southwest       SW      Joseph Dalsass  2.7     .8      2       18

    #在sed處理文件的時候,每一行都被保存在pattern space的臨時緩沖區(qū)中。除非行被刪除或者輸出被取消,否則所有被處理過的行都將打印在屏幕上。接著pattern space被清空,并存入新的一行等待處理。在下面的例子中,包含模板的northeast行被找到,并被放入pattern space中,h命令將其復(fù)制并存入一個稱為holding buffer的特殊緩沖區(qū)內(nèi)。在第二個sed編輯命令中,當(dāng)達(dá)到最后一行后,G命令告訴sed從holding buffer中取得該行,然后把它放回到pattern space中,且追加到現(xiàn)在已經(jīng)存在于模式空間的行的末尾。
     /> sed -e '/northeast/h' -e '$G' testfile
    northwest       NW     Charles Main            3.0    .98     3       34
    western          WE     Sharon Gray            5.3    .97     5       23
    southwest       SW    Lewis Dalsass          2.7     .8       2       18
    southern         SO     Suan Chin               5.1     .95     4       15
    southeast       SE      Patricia Hemenway   4.0     .7       4       17
    eastern           EA      TB Savage              4.4     .84     5       20
    northeast       NE      AM Main Jr.              5.1     .94     3       13
    north             NO      Margot Weber         4.5     .89     5       9
    central           CT      Ann Stephens          5.7     .94     5       13
    northeast       NE      AM Main Jr.              5.1     .94     3       13

    #如果模板WE在某一行被匹配,h命令將使得該行從pattern space中復(fù)制到holding buffer中,d命令在將該行刪除,因此WE匹配行沒有在原來的位置被輸出。第二個命令搜索CT,一旦被找到,G命令將從holding buffer中取回行,并追加到當(dāng)前pattern space的行末尾。簡單的說,WE所在的行被移動并追加到包含CT行的后面。
    /> sed -e '/WE/{h;d;}' -e '/CT/{G;}' testfile
    northwest       NW    Charles Main           3.0     .98     3       34
    southwest       SW    Lewis Dalsass         2.7     .8      2       18
    southern         SO     Suan Chin              5.1     .95     4       15
    southeast       SE      Patricia Hemenway   4.0     .7      4       17
    eastern           EA     TB Savage              4.4     .84     5       20
    northeast       NE      AM Main Jr.              5.1     .94     3       13
    north             NO      Margot Weber         4.5     .89     5       9
    central           CT      Ann Stephens          5.7     .94     5       13
    western         WE      Sharon Gray           5.3     .97     5       23

    #第一個命令將匹配northeast的行從pattern space復(fù)制到holding buffer,第二個命令在讀取的文件的末尾時,g命令告訴sed從holding buffer中取得行,并把它放回到pattern space中,以替換已經(jīng)存在于pattern space中的。簡單說就是包含模板northeast的行被復(fù)制并覆蓋了文件的末尾行。
    /> sed -e '/northeast/h' -e '$g' testfile
    northwest       NW     Charles Main          3.0     .98     3       34
    western          WE      Sharon Gray         5.3     .97      5       23
    southwest       SW     Lewis Dalsass        2.7     .8       2       18
    southern         SO      Suan Chin             5.1     .95     4       15
    southeast       SE      Patricia Hemenway   4.0     .7      4       17
    eastern           EA      TB Savage             4.4     .84     5       20
    northeast       NE      AM Main Jr.             5.1     .94     3       13
    north             NO      Margot Weber        4.5     .89     5       9
    northeast       NE      AM Main Jr.             5.1     .94     3       13

    #模板WE匹配的行被h命令復(fù)制到holding buffer,再被d命令刪除。結(jié)果可以看出WE的原有位置沒有輸出。第二個編輯命令將找到匹配CT的行,g命令將取得holding buffer中的行,并覆蓋當(dāng)前pattern space中的行,即匹配CT的行。簡單的說,任何包含模板northeast的行都將被復(fù)制,并覆蓋包含CT的行。    
    /> sed -e '/WE/{h;d;}' -e '/CT/{g;}' testfile
    northwest       NW    Charles Main           3.0     .98      3      34
    southwest       SW    Lewis Dalsass         2.7     .8       2       18
    southern         SO     Suan Chin              5.1     .95      4      15
    southeast       SE      Patricia Hemenway   4.0     .7       4      17
    eastern          EA      TB Savage              4.4     .84      5      20
    northeast       NE      AM Main Jr.              5.1     .94     3      13
    north             NO      Margot Weber        4.5     .89      5      9
    western         WE      Sharon Gray           5.3     .97     5      23

    #第一個編輯中的h命令將匹配Patricia的行復(fù)制到holding buffer中,第二個編輯中的x命令,會將holding buffer中的文本考慮到pattern space中,而pattern space中的文本被復(fù)制到holding buffer中。因此在打印匹配Margot行的地方打印了holding buffer中的文本,即第一個命令中匹配Patricia的行文本,第三個編輯命令會將交互后的holding buffer中的文本在最后一行的后面打印出來。
     /> sed -e '/Patricia/h' -e '/Margot/x' -e '$G' testfile
    northwest       NW      Charles Main           3.0      .98      3       34
    western           WE      Sharon Gray           5.3     .97      5       23
    southwest       SW      Lewis Dalsass         2.7      .8       2       18
    southern         SO      Suan Chin               5.1      .95     4       15
    southeast       SE       Patricia Hemenway    4.0      .7       4       17
    eastern           EA      TB Savage               4.4      .84     5       20
    northeast       NE       AM Main Jr.               5.1     .94      3       13
    southeast       SE      Patricia Hemenway      4.0     .7       4       17
    central            CT      Ann Stephens            5.7     .94     5       13

標(biāo)簽:衡陽 慶陽 仙桃 湘西 六盤水 衡陽 三門峽 茂名

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《linux sed命令詳解》,本文關(guān)鍵詞  linux,sed,命令,詳解,linux,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《linux sed命令詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于linux sed命令詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    邳州市| 邹城市| 常州市| 林芝县| 望都县| 乌兰浩特市| 合江县| 大安市| 六安市| 永新县| 尚志市| 和顺县| 泸定县| 孟州市| 无极县| 大竹县| 上林县| 大兴区| 通城县| 长武县| 读书| 普陀区| 南江县| 青海省| 南宫市| 商丘市| 星子县| 婺源县| 海伦市| 徐州市| 伊通| 达孜县| 施秉县| 岗巴县| 广丰县| 东城区| 都匀市| 海安县| 兰溪市| 任丘市| 绿春县|