高效而輕鬆的sed命令

來源:互聯網
上載者:User

高效而輕鬆的sed命令
sed(stream editor)是一款高效的流編輯器,它一次只處理一行內容,處理時,把當前處理的行儲存在臨時緩衝區中,稱為“模式空間”(pattern space),接著用sed命令處理緩衝區中的內容,處理完成後把緩衝區的內容送往螢幕,接著處理下一行,這樣不斷重複,直到檔案末尾。sed處理過的檔案內容並沒有改變,除非你使用重新導向儲存輸出。sed命令的使用規則

命令格式如下:

sed [option] 'command' input_file

其中option是可選的,常用的option有如下幾種:

-n 使用安靜(silent)模式,只列出經過sed特殊處理的那一行(或者動作)內容;-e 直接在指令列模式上進行 sed 的動作編輯;-f 直接將 sed 的動作寫在一個檔案內, -f filename 則可以執行filename內的sed命令;-r 讓sed命令支援擴充的Regex(預設是基礎Regex);-i 直接修改讀取的檔案內容,而不是由螢幕輸出;

常用的命令有以下幾種:

a \: 即append追加字串,可將其後的字元加在所選擇內容的後面c \: 取代/替換字串,可將其後內容替換至所選內容d : 即delete刪除,該命令會將當前選中的行刪除i \: 即insert插入字串,可將其後內容插入至所選內容前p : print即列印,該命令會列印當前選擇的行到螢幕上s : 替換,通常s命令的用法是這樣的:1,2s/old/new/g,將old字串替換成new字串

命令樣本

假設有一個本地檔案test.txt,檔案內容如下:

[root@linuxprobe ~]$ cat test.txtthis is first linethis is second linethis is third linethis is fourth linethis fifth linehappy everydayend

本節將使用該檔案詳細示範每一個命令的用法。

a命令
[root@linuxprobe ~]$ sed '1a \add one' test.txtthis is first lineadd onethis is secondlinethis is third linethis is fourth linethis is fifth linehappy everydayend

本例命令部分中的1表示第一行,同樣的第二行寫成2,第一行到第三行寫成1,3,用$表示最後一行,比如2,$表示第二行到最後一行中間所有的行(包含第二行和最後一行)。
本例的作用是在第一行之後增加字串”add one”,從輸出可以看到具體效果。

[root@linuxprobe ~]$ sed '1,$a \add one' test.txtthis is first lineadd onethis is second lineadd onethis is third lineadd onethis is fourth lineadd onethis is fifth lineadd onehappy everydayadd oneendadd one

本例表示在第一行和最後一行所有的行後面都加上”add one”字串,從輸出可以看到效果。

[root@linuxprobe ~]$ sed '/first/a \add one' test.txtthis is first lineadd onethis is secondlinethis is third linethis is fourth linethis is fifth linehappy everydayend

本例表示在包含”first”字串的行的後面加上字串”add one”,從輸出可以看到第一行包含first,所以第一行之後增加了”add one”

[root@linuxprobe ~]$ sed '/^ha.*day$/a \add one' test.txtthis is first linethis is secondlinethis is third linethis is fourth linethis is fifth linehappy everydayadd oneend

本例使用Regex匹配行,^ha.*day$表示以ha開頭,以day結尾的行,則可以匹配到檔案的”happy everyday”這樣,所以在該行後面增加了”add one”字串。

i命令

i命令使用方法和a命令一樣的,只不過是在匹配的行的前面插入字串,所以直接將上面a命令的樣本的a替換成i即可,在此就不囉嗦了。

c命令
[root@linuxprobe ~]$ sed '$c \add one' test.txtthis is first linethis is secondlinethis is third linethis is fourth linethis is     fifth linehappy everydayadd one

本例表示將最後一行替換成字串”add one”,從輸出可以看到效果。

[root@linuxprobe ~]$ sed '4,$c \add one' test.txtthis is first linethis is secondlinethis is third lineadd one

本例將第四行到最後一行的內容替換成字串”add one”。

[root@linuxprobe ~]$ sed '/^ha.*day$/c \replace line' test.txtthis is first linethis is secondlinethis is third linethis is fourth linethis is fifth linereplace lineend

本例將以ha開頭,以day結尾的行替換成”replace line”。

d命令
[root@linuxprobe ~]$ sed '/^ha.*day$/d' test.txtthis isfirst linethis issecond linethis isthird linethis isfourth linethis isfifth lineend

本例刪除以ha開頭,以day結尾的行。

[root@linuxprobe ~]$ sed '4,$d' test.txtthisis first linethisis second linethisis third line

本例刪除第四行到最後一行中的內容。

p命令
[root@linuxprobe ~]$ sed -n '4,$p' test.txtthisis fourth linethisis fifth linehappy everydayend

本例在螢幕上列印第四行到最後一行的內容,p命令一般和-n選項一起使用。

[root@linuxprobe ~]$ sed -n '/^ha.*day$/p' test.txthappy everyday

本例列印以ha開始,以day結尾的行。

s命令

實際運用中s命令式最常使用到的。

[root@linuxprobe ~]$ sed 's/line/text/g' test.txtthis isfirst textthis issecond textthis isthird textthis isfourth textthis isfifth texthappy everydayend

本例將檔案中的所有line替換成text,最後的g是global的意思,也就是全域替換,如果不加g,則只會替換本行的第一個line。

[root@linuxprobe ~]$ sed '/^ha.*day$/s/happy/very happy/g' test.txtthis isfirst linethis issecond linethis isthird linethis isfourth linethis isfifth linevery happy everydayend

本例首先匹配以ha開始,以day結尾的行,本例中匹配到的行是”happy everyday”這樣,然後再將該行中的happy替換成very happy。

[root@linuxprobe ~]$ sed 's/\(.*\)line$/\1/g' test.txtthisis firstthisis secondthisis thirdthisis fourththisis fifthhappy everydayend

這個例子有點複雜,先分解一下。首先s命令的模式是s/old/new/g這樣的,所以本例的old部分即\(.*\)line$,sed命令中使用\(\)包裹的內容表示Regex的第n部分,序號從1開始計算,本例中只有一個\(\)所以\(.*\)表示Regex的第一部分,這部分匹配任一字元串,所以\(.*\)line$匹配的就是以line結尾的任何行。然後將匹配到的行替換成Regex的第一部分(本例中相當於刪除line部分),使用\1表示匹配到的第一部分,同樣\2表示第二部分,\3表示第三部分,可以依次這樣引用。比如下面的例子:

[root@linuxprobe ~]$ sed 's/\(.*\)is\(.*\)line/\1\2/g' test.txtthis  firstthis  secondthis  thirdthis  fourththis  fifthhappy everydayend

Regex中is兩邊的部分可以用\1和\2表示,該例子的作用其實就是刪除中間部分的is。



轉載地址:http://www.linuxprobe.com


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.