1. 程式人生 > >Linux Shell常用技巧(三)

Linux Shell常用技巧(三)

 

 

 

 

 

sed一次處理一行檔案並把輸出送往螢幕。sed把當前處理的行儲存在臨時緩衝區中,稱為模式空間(pattern space)。一旦sed完成對模式空間中的行的處理,模式空間中的行就被送往螢幕。行被處理完成之後,就被移出模式空間,程式接著讀入下一行,處理,顯示,移出......檔案輸入的最後一行被處理完以後sed結束。通過儲存每一行在臨時緩衝區,然後在緩衝區中操作該行,保證了原始檔案不會被破壞。

   

   1. sed的命令和選項:

命令功能描述

a\ 在當前行的後面加入一行或者文字。c\ 用新的文字改變或者替代本行的文字。d 從pattern space位置刪除行。i\ 在當前行的上面插入文字。h 拷貝pattern space的內容到holding buffer(特殊緩衝區)。H 追加pattern space的內容到holding buffer。g 獲得holding buffer中的內容,並替代當前pattern space中的文字。G 獲得holding buffer中的內容,並追加到當前pattern space的後面。n 讀取下一個輸入行,用下一個命令處理新的行而不是用第一個命令。p 列印pattern space中的行。P 列印pattern space中的第一行。q 退出sed。w file 寫並追加pattern space到file的末尾。! 表示後面的命令對所有沒有被選定的行發生作用。s/re/string 用string替換正則表示式re。= 列印當前行號碼。替換標記
 g 行內全面替換,如果沒有g,只替換第一個匹配。p 列印行。x 互換pattern space和holding buffer中的文字。y 把一個字元翻譯為另一個字元(但是不能用於正則表示式)。選項 -e 允許多點編輯。-n 取消預設輸出。

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

   2. sed例項:

   /> 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選項取消了sed的預設行為。在沒有-n的時候,包含模板的行被列印兩次,但是在使用-n的時候將只打印包含模板的行。

  /> 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  #第三行被刪除,其他行預設輸出到螢幕。

   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表示命令作用於整個當前行。如果該行存在多個west,都將被替換為north,如果沒有g,則只是替換第一個匹配。

    /> 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

   #&符號表示替換字串中被找到的部分。所有以兩個數字結束的行,最後的數字都將被它們自己替換,同時追加.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選項加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後面的字元一定是分隔搜尋字串和替換字串的分隔符,預設為斜槓,但是在s命令使用的情況下可以改變。不論什麼字元緊跟著s命令都認為是新的分隔符。這個技術在搜尋含斜槓的模板時非常有用,例如搜尋時間和路徑的時候。

    /> 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所確定的範圍內的行都被列印,如果west出現在esst後面的行中,從west開始到下一個east,無論這個east出現在哪裡,二者之間的行都被列印,即使從west開始到檔案的末尾還沒有出現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選項表示多點編輯。第一個編輯命令是刪除第一到第三行。第二個編輯命令是用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的行後,執行後面花括號中的一組命令,每個命令之間用逗號分隔,n表示定位到匹配行的下一行,s/AM/Archie/完成Archie到AM的替換,p和-n選項的合用,則只是列印作用到的行。

    /> sed -n '/eastern/{n;s/AM/Archie/;p}' testfile

   northeast      NE     Archie Main Jr. 5.1    .94    3      13

   #-e表示多點編輯,第一個編輯命令y將前三行中的所有小寫字母替換為大寫字母,-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

   #當模板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的臨時緩衝區中。除非行被刪除或者輸出被取消,否則所有被處理過的行都將列印在螢幕上。接著pattern space被清空,並存入新的一行等待處理。在下面的例子中,包含模板的northeast行被找到,並被放入pattern space中,h命令將其複製並存入一個稱為holding buffer的特殊緩衝區內。在第二個sed編輯命令中,當達到最後一行後,G命令告訴sed從holding buffer中取得該行,然後把它放回到pattern space中,且追加到現在已經存在於模式空間的行的末尾。

     /> 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中複製到holding buffer中,d命令在將該行刪除,因此WE匹配行沒有在原來的位置被輸出。第二個命令搜尋CT,一旦被找到,G命令將從holding buffer中取回行,並追加到當前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複製到holding buffer,第二個命令在讀取的檔案的末尾時,g命令告訴sed從holding buffer中取得行,並把它放回到pattern space中,以替換已經存在於pattern space中的。簡單說就是包含模板northeast的行被複制並覆蓋了檔案的末尾行。

    /> 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命令複製到holding buffer,再被d命令刪除。結果可以看出WE的原有位置沒有輸出。第二個編輯命令將找到匹配CT的行,g命令將取得holding buffer中的行,並覆蓋當前pattern space中的行,即匹配CT的行。簡單的說,任何包含模板northeast的行都將被複制,並覆蓋包含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的行復制到holding buffer中,第二個編輯中的x命令,會將holding buffer中的文字考慮到pattern space中,而pattern space中的文字被複制到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

點選瞭解更多,瞭解更多哦