sed 命令詳解

sed 即 stream editor,一個簡單而強大的文本解析轉換工具,1973-1974年期間由貝爾實驗室的 Lee E. McMahon 開發,能夠完美的配合正則表達式使用。
處理文本時,當前處理的行會先存儲在臨時緩沖區中,稱為模式空間(pattern space),操作完成后再把緩沖區的內容送往屏幕。接著處理下一行,直到文件末尾。文件內容并沒有改變,除非使用重定向存儲輸出。
sed 主要用來自動編輯一個或多個文件,簡化對文件的反復操作,編寫轉換程序等。

1. 使用 s 命令替換

基本使用

sed 默認情況下通過 STDIN 輸入流讀取數據,可以利用管道符(|)進行重定向。如下:

$ echo "This is a test" | sed 's/test/big test/'
This is a big test

s 替換命令的格式為:s/pattern/replacement/flags ,如下:

$ cat test.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
$
$ sed 's/dog/cat/' test.txt
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy cat.

使用 s 命令進行替換時,默認只替換每行中的第一個匹配。全局替換需增加 g 選項。

$ sed 's/o/O/g' test.txt
The quick brOwn fOx jumps Over the lazy dOg.
The quick brOwn fOx jumps Over the lazy dOg.
The quick brOwn fOx jumps Over the lazy dOg.
The quick brOwn fOx jumps Over the lazy dOg.

sed 默認只將處理后的內容輸出,而不對原文件的內容做任何改動。如需寫入文件,可使用重定向:$ sed 's/dog/cat/' test.txt > test2.txt
或通過 -i 選項直接修改文件內容:$ sed -i 's/dog/cat/' test.txt

address

如果需要將命令應用到特定的一行或多行內容,可以使用 line addressing。格式為 [address[,address]][!]command
address 可以是數字模式,也可以通過逗號分隔兩個 address 表示一個區間范圍。
如只將第2行中的 dog 替換為 cat

$ sed '2s/dog/cat/' test.txt
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy cat.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

或者$ sed '1,3s/dog/cat/' test.txt,替換1-3行的內容。
$ sed '2,$s/dog/cat/' test.txt,替換第2行到最后一行的內容。

! 表示完成匹配后是否在該行執行替換命令。加上 ! 表示不執行。如下:

$ cat lines.txt
This is line one
This is line two
This is line three
This is line four
$
$ sed '/one/,2s/line/LINE/' lines.txt
This is LINE one
This is LINE two
This is line three
This is line four
$
$ sed '/one/,2!s/line/LINE/' lines.txt
This is line one
This is line two
This is LINE three
This is LINE four
替換選項

sed 的替換命令除了可以附加 g 選項(全局替換)外,還有更多選項適用于不同的情形。

  • 數字。表示只替換每行中的第 n 個匹配項,如:
$ sed 's/o/O/2' test.txt
The quick brown fOx jumps over the lazy dog.
The quick brown fOx jumps over the lazy dog.
The quick brown fOx jumps over the lazy dog.
The quick brown fOx jumps over the lazy dog.

$ sed 's/o/O/2g' test.txt 替換每行中從第二個開始的所有匹配項(即替換每行中從第二個 o 開始直到該行行尾的所有 o 字符)

  • p,表示打印處理前的原始內容,結合上 -n 選項(不打印輸出)則可以只輸出處理過的內容。
$ sed 's/three/3/p' lines.txt
This is line one
This is line two
This is line 3
This is line 3
This is line four
$
$ sed -n 's/three/3/p' lines.txt
This is line 3
  • w,將處理過的內容寫入文件
$ sed 's/three/3/w line3.txt' lines.txt
$
$ cat line3.txt
This is line 3
sed 命令選項
  • 刪除:d
    $ sed '2,$d' lines.txt 刪除 lines.txt 中第2行到最后一行的內容。
    $ sed '/two/d' lines.txt 刪除 lines.txt 中包含 two 的行。
  • 追加(行下):a
$ sed '/two/a\
> This is a line behind line 2' lines.txt
This is line one
This is line two
This is a line behind line 2
This is line three
This is line four
  • 插入(行上):i
$ sed '2i\
> This is a line above line 2' lines.txt

在第2行以上插入內容。

  • 修改:c
$ sed '/two/c\
> Line 2' lines.txt

將包含 two 的行修改為 Line 2(整行內容替換為 Line 2)。

  • 字符轉換:y
    格式為:[address]y/inchars/outchars/
    輸入文件中所有包含在 inchars 中的字符都將替換為 outchars 中對應的字符。
$ cat line_number.txt
This is line 1.
This is line 2.
This is line 3.
This is another line 1.
$
$ sed 'y/123/456/' line_number.txt
This is line 4.
This is line 5.
This is line 6.
This is another line 4.
其他用法
  • 打印輸出
    sed 的 p 命令可以達到類似 grep 的效果,結合上 address 功能還可以完成更復雜的篩選打印操作。
$ sed -n 'p' lines.txt
This is line one
This is line two
This is line three
This is line four
$
$ sed -n '2,3p' lines.txt
This is line two
This is line three
$
$sed -n '/two/,/three/p' lines.txt
This is line two
This is line three
  • 多個匹配
    可以通過 -e 選項實現多個匹配的替換
$ sed -e 's/fox/kangaroo/;s/dog/cat/' test.txt
The quick brown kangaroo jumps over the lazy cat.
The quick brown kangaroo jumps over the lazy cat.
The quick brown kangaroo jumps over the lazy cat.
The quick brown kangaroo jumps over the lazy cat.

也可以這樣:

$ sed -e '
> s/brown/red/
> s/fox/kangaroo/
> s/dog/cat/' test.txt
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
  • 從腳本文件中讀取命令
$ cat script.sed
s/brown/red/
s/fox/kangaroo/
s/dog/cat
$
$ sed -f script.sed test.txt
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
  • 可以使用 & 變量表示前面的匹配項,如:
$ sed 's/fox/&es/' test.txt
The quick brown foxes jumps over the lazy dog.
The quick brown foxes jumps over the lazy dog.
The quick brown foxes jumps over the lazy dog.
The quick brown foxes jumps over the lazy dog.

參考書籍和文章:

Linux Command Line and Shell Scripting Bible, 3rd Edition
SED 簡明教程 by 陳皓
Linux 命令大全 / SED 命令

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,983評論 6 537
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,772評論 3 422
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,947評論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,201評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,960評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,350評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,406評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,549評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,104評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,914評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,089評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,647評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,340評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,753評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,007評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,834評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,106評論 2 375

推薦閱讀更多精彩內容

  • 原文鏈接:sed命令_Linux sed 命令用法詳解:功能強大的流式文本編輯器 http://man.linu...
    e2ae5d4bd7c1閱讀 812評論 0 1
  • linux sed命令詳解 1. Sed簡介 sed 是一種在線編輯器,它一次處理一行內容。處理時,把當前處理的行...
    很少更新了閱讀 2,169評論 0 6
  • shell sed命令詳解 sed是一種在線編輯器,它一次處理一行內容。處理時,把當前處理的行存儲在臨時緩沖區中...
    zplodge閱讀 1,931評論 0 0
  • 1. 簡介 sed命令是一個很強大的文本編輯器,可以對來自文件、以及標準輸入的文本進行編輯。 執行時,sed會從文...
    tyrone_li閱讀 21,645評論 2 12
  • 本文承接之前寫的三十分鐘學會AWK一文,在學習完AWK之后,趁熱打鐵又學習了一下SED,不得不說這兩個工具真的堪稱...
    mylxsw閱讀 4,412評論 3 74