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程序腳本的起始,終端會繼續提示你輸入更多命令,直到輸入了標示出結束的單引號。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章