Linux下的sed流編輯器命令詳解

2021-09-08 19:08:39 字數 4858 閱讀 2329

sed是stream editor的簡稱,也就是流編輯器。它一次處理一行內容,處理時,把當前處理的行儲存在臨時緩衝區中,稱為「模式空間」(pattern space),接著用sed命令處理緩衝區中的內容,處理完成後,把緩衝區的內容送往螢幕。接著處理下一行,這樣不斷重複,直到檔案末尾。檔案內容並沒有 改變,除非你使用重定向儲存輸出。

sed命令的使用規則是這樣的:

sed [option] '

command

' input_file

動作說明: [n1[,n2]]function

n1, n2 :不見得會存在,一般代表『選擇進行動作的行數』,舉例來說,如果我的動作是需要在 10 到 20 行之間進行的,則『 10,20[動作行為] 』

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

[root@master rh]# cat

test.txt

this is first line

this is second line

this is third line

this is fourth line

this fifth line

end

[root@master rh]# sed

'1a \add one

'test.txt

this is first line

add one

this is second line

this is third line

this is fourth line

this fifth line

end

本例命令部分中的1表示第一行,同樣的第二行寫成2,第一行到第三行寫成1,3,用$表示最後一行,比如2,$表示第二行到最後一行中間所有的行(包含第二行和最後一行)。

本例的作用是在第一行之後增加字串」add one」,從輸出可以看到具體效果。

[root@master rh]# sed

'1,$a \add one

'test.txt

this is first line

add one

this is second line

add one

this is third line

add one

this is fourth line

add one

this fifth line

add one

add one

endadd one

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

[root@master rh]# sed

'/first/a \add one

'test.txt

this is first line

add one

this is second line

this is third line

this is fourth line

this fifth line

end

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

[root@master rh]# sed

'/^ha.*day$/a \add one

'test.txt

this is first line

this is second line

this is third line

this is fourth line

this fifth line

add one

end

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

[root@master rh]# sed

'$c \add one

'test.txt

this is first line

this is second line

this is third line

this is fourth line

this fifth line

add one

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

[root@master rh]# sed

'4,$c \add one

'test.txt

this is first line

this is second line

this is third line

add one

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

[root@master rh]# sed

'/^ha.*day$/c \replace line

'test.txt

this is first line

this is second line

this is third line

this is fourth line

this fifth line

replace line

end

[root@master rh]# sed

'/^ha.*day$/d

'test.txt

this is first line

this is second line

this is third line

this is fourth line

this fifth line

end

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

[root@master rh]# sed

'4,$d

'test.txt

this is first line

this is second line

this is third line

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

[root@master rh]# sed -n '

4,$p

'test.txt

this is fourth line

this fifth line

end

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

[root@master rh]# sed -n '

/^ha.*day$/p

'test.txt

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

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

[root@master rh]# sed

's/line/text/g

'test.txt

this is first text

this is second text

this is third text

this is fourth text

this fifth text

end

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

[root@master rh]# sed''

test.txt

this is first line

this is second line

this is third line

this is fourth line

this fifth line

end

[root@master rh]# sed

's/\(.*\)line$/\1/g

'test.txt

this is first

this is second

this is third

this is fourth

this fifth

end

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

[root@master rh]# sed

's/\(.*\)is\(.*\)line/\1\2/g

'test.txt

this first

this second

this third

this fourth

th fifth

end

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

sed流編輯器

sed預設不編輯原始檔,僅對模式空間中的資料做處理 而後,處理結束後,將模式空間中的內容列印至螢幕。sed options addresscommand file.用行,和命令一起來操作文字 options n 靜默顯示,不再顯示模式空間中的內容 i 直接修改原檔案 e 指令碼 e指令碼 可以同時執...

sed 流編輯器

sed 模式空間 預設不編輯原檔案,僅對模式空間中的資料做處理 而後,處理結束後,將模式空間列印至螢幕 sed options addresscommand file n 靜默模式,不再預設顯示模式空間中的內容 i 直接修改原檔案 e script e script 可以同時執行多個指令碼 f pa...

Linux基礎 sed 流編輯器

sed stream editer 流編輯器 sed r 擴充套件正澤 可以用小括號 n 靜默模式 不但輸出 i 直接寫入 sed 3d test 第三行 sed 3c 11111 test 第三行改成11111 sed 1d 3d test 刪除第一行 第三行 sed 1,3d test 刪除第一...