1. 程式人生 > >sed編輯器基礎——s命令

sed編輯器基礎——s命令

sed編輯器簡介

sed編輯器是流編輯器(stream editor),sed編輯器不會修改原檔案中的內容。

sed編輯器操作步驟

  1. 一次從輸入中讀取一行資料。
  2. 根據所提供的編輯器命令匹配資料。
  3. 按照命令修改流中的資料。
  4. 將新的資料輸出到STDOUT。

sed編輯器基礎

sed的s命令

s命令的格式:
s/pattern/replacement/flags
s命令會用斜線間指定的第二個文字字串來替換第一個文字字串模式。

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

s命令也可以編輯整個檔案,準備一個data1.txt檔案

-> cat data1.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 monkey.
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 's/dog/cat/' data1.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 monkey.
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.

從結果中可以看到所有的dog->cat了。

在命令列中使用多個編輯器命令

-> sed -e 's/dog/cat/;s/monkey/dog/' data1.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 dog.
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.

從檔案中讀取編輯器命令

準備的指令碼檔案如下:

-> cat script1.sed
s/brown/green/
s/fox/elephant/
s/dog/cat/
-> sed -f script1.sed data1.txt
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy monkey.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.

s命令中的flags

替換命令在替換多行的文字時能正常工作,預設情況下只替換每行中出現的第一處(當flags為空的時候)。要讓替換命令能夠替換一行中不同的地方出現的文字必須使用替換標記(flags)。
替換標記:
1. 數字,表明新文字將替換第幾處模式匹配的地方;
2. g,表明新文體將會替換所有匹配的文字;
3. p,表明匹配的行要打印出來;
4. w file,將替換的結果寫到檔案中。

-> cat data2.txt
This is a test of the script.
This is the second test of the test script.
# 指定sed編輯器用新文字替換第幾處模式匹配的地方
—> sed 's/test/trial/2' data2.txt
This is a test of the script.
This is the second test of the trial script.
# 匹配全部(g)
-> sed 's/test/trial/g' data2.txt
This is a trial of the script.
This is the second trial of the trial script.
# p會列印與替換命令中指定的模式匹配的行
-> cat data3.txt
This is a test line.
This is a different line.
-> sed 's/test/trial/' data3.txt
This is a trial line.
This is a different line.
# 加上p輸出
-> sed 's/test/trial/p' data3.txt
This is a trial line.
This is a trial line.
This is a different line.
# -n選項禁止sed編輯器輸出;-n和p只輸出被替換命令修改過的行。
-> sed -n 's/test/trial/p' data3.txt
This is a trial line.
# sed編輯器的正常輸出是在STDOUT中,只有包含匹配模式的行才會儲存在指定的輸出檔案中
-> sed 's/test/trial/w test.txt' data3.txt
This is a trial line.
This is a different line.
-> cat test.txt
This is a trial line.

s命令中的字元

-> cat data4.txt
/bin/bash/yangyun
/bin/bash/home
# sed編輯器允許選擇其他字元來作為替換命令中的字串分隔符
-> sed 's!/bin/bash!/bin/zsh!' data4.txt
/bin/zsh/yangyun
/bin/zsh/home
# 轉義字元"\",不過看起來沒有上面的清晰
-> sed 's/\/bin\/bash/\/bin\/zsh/' data4.txt
/bin/zsh/yangyun
/bin/zsh/home

sed的次提示符

-> sed -e '
> s/brown/green/
> s/fox/elephant/
> s/dog/cat/' data1.txt
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy monkey.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.

只要輸入第一個單引號標示出sed程式指令碼的起始,終端會繼續提示你輸入更多命令,直到輸入了標示出結束的單引號。