sed 命令詳解

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

1. 使用 s 命令替換

基本使用

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

$ 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 命令進(jìn)行替換時(shí),默認(rèn)只替換每行中的第一個(gè)匹配。全局替換需增加 g 選項(xiàng)。

$ 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 默認(rèn)只將處理后的內(nèi)容輸出,而不對原文件的內(nèi)容做任何改動(dòng)。如需寫入文件,可使用重定向:$ sed 's/dog/cat/' test.txt > test2.txt
或通過 -i 選項(xiàng)直接修改文件內(nèi)容:$ sed -i 's/dog/cat/' test.txt

address

如果需要將命令應(yīng)用到特定的一行或多行內(nèi)容,可以使用 line addressing。格式為 [address[,address]][!]command
address 可以是數(shù)字模式,也可以通過逗號分隔兩個(gè) address 表示一個(gè)區(qū)間范圍。
如只將第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行的內(nèi)容。
$ sed '2,$s/dog/cat/' test.txt,替換第2行到最后一行的內(nèi)容。

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

$ 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
替換選項(xiàng)

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

  • 數(shù)字。表示只替換每行中的第 n 個(gè)匹配項(xiàng),如:
$ 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 替換每行中從第二個(gè)開始的所有匹配項(xiàng)(即替換每行中從第二個(gè) o 開始直到該行行尾的所有 o 字符)

  • p,表示打印處理前的原始內(nèi)容,結(jié)合上 -n 選項(xiàng)(不打印輸出)則可以只輸出處理過的內(nèi)容。
$ 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,將處理過的內(nèi)容寫入文件
$ sed 's/three/3/w line3.txt' lines.txt
$
$ cat line3.txt
This is line 3
sed 命令選項(xiàng)
  • 刪除:d
    $ sed '2,$d' lines.txt 刪除 lines.txt 中第2行到最后一行的內(nèi)容。
    $ 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行以上插入內(nèi)容。

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

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

  • 字符轉(zhuǎn)換:y
    格式為:[address]y/inchars/outchars/
    輸入文件中所有包含在 inchars 中的字符都將替換為 outchars 中對應(yīng)的字符。
$ 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 命令可以達(dá)到類似 grep 的效果,結(jié)合上 address 功能還可以完成更復(fù)雜的篩選打印操作。
$ 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
  • 多個(gè)匹配
    可以通過 -e 選項(xiàng)實(shí)現(xiàn)多個(gè)匹配的替換
$ 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.
  • 可以使用 & 變量表示前面的匹配項(xiàng),如:
$ 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 命令

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 原文鏈接:sed命令_Linux sed 命令用法詳解:功能強(qiáng)大的流式文本編輯器 http://man.linu...
    e2ae5d4bd7c1閱讀 816評論 0 1
  • linux sed命令詳解 1. Sed簡介 sed 是一種在線編輯器,它一次處理一行內(nèi)容。處理時(shí),把當(dāng)前處理的行...
    很少更新了閱讀 2,170評論 0 6
  • shell sed命令詳解 sed是一種在線編輯器,它一次處理一行內(nèi)容。處理時(shí),把當(dāng)前處理的行存儲在臨時(shí)緩沖區(qū)中...
    zplodge閱讀 1,945評論 0 0
  • 1. 簡介 sed命令是一個(gè)很強(qiáng)大的文本編輯器,可以對來自文件、以及標(biāo)準(zhǔn)輸入的文本進(jìn)行編輯。 執(zhí)行時(shí),sed會從文...
    tyrone_li閱讀 21,677評論 2 12
  • 本文承接之前寫的三十分鐘學(xué)會AWK一文,在學(xué)習(xí)完AWK之后,趁熱打鐵又學(xué)習(xí)了一下SED,不得不說這兩個(gè)工具真的堪稱...
    mylxsw閱讀 4,417評論 3 74