[root@localhost ~]# sed [選項] [腳本命令] 文件名
該命令常用的選項及含義,如表 1 所示。| 選項 | 含義 |
|---|---|
| -e 腳本命令 | 該選項會將其后跟的腳本命令添加到已有的命令中。 |
| -f 腳本命令文件 | 該選項會將其后文件中的腳本命令添加到已有的命令中。 |
| -n | 默認情況下,sed 會在所有的腳本指定執行完畢后,會自動輸出處理后的內容,而該選項會屏蔽啟動輸出,需使用 print 命令來完成輸出。 |
| -i | 此選項會直接修改源文件,要慎用。 |
[address]s/pattern/replacement/flags
其中,address 表示指定要操作的具體行,pattern 指的是需要替換的內容,replacement 指的是要替換的新內容。關于指定具體操作行(address)的用法,這里先不做解釋,文章后續會對其做詳細介紹。
此命令中常用的 flags 標記如表 2 所示。| flags 標記 | 功能 |
|---|---|
| n | 1~512 之間的數字,表示指定要替換的字符串出現第幾次時才進行替換,例如,一行中有 3 個 A,但用戶只想替換第二個 A,這是就用到這個標記; |
| g | 對數據中所有匹配到的內容進行替換,如果沒有 g,則只會在第一次匹配成功時做替換操作。例如,一行數據中有 3 個 A,則只會替換第一個 A; |
| p | 會打印與替換命令中指定的模式匹配的行。此標記通常與 -n 選項一起使用。 |
| w file | 將緩沖區中的內容寫到指定的 file 文件中; |
| & | 用正則表達式匹配的內容進行替換; |
| /n | 匹配第 n 個子串,該子串之前在 pattern 中用 /(/) 指定。 |
| / | 轉義(轉義替換部分包含:&、/ 等)。 |
[root@localhost ~]# sed 's/test/trial/2' data4.txt
This is a test of the trial script.
This is the second test of the trial script.
[root@localhost ~]# sed 's/test/trial/g' data4.txt
This is a trial of the trial script.
This is the second trial of the trial script.
[root@localhost ~]# cat data5.txt
This is a test line.
This is a different line.
[root@localhost ~]# sed -n 's/test/trial/p' data5.txt
This is a trial line.
[root@localhost ~]# sed 's/test/trial/w test.txt' data5.txt
This is a trial line.
This is a different line.
[root@localhost ~]#cat test.txt
This is a trial line.
[root@localhost ~]# sed 's///bin//bash///bin//csh/' /etc/passwd
[address]d
如果需要刪除文本中的特定行,可以用 d 腳本命令,它會刪除指定行中的所有內容。但使用該命令時要特別小心,如果你忘記指定具體行的話,文件中的所有內容都會被刪除,舉個例子:[root@localhost ~]# 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 dog
The quick brown fox jumps over the lazy dog
[root@localhost ~]# sed 'd' data1.txt
#什么也不輸出,證明成了空文件
[root@localhost ~]# cat data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
[root@localhost ~]# sed '3d' data6.txt
This is line number 1.
This is line number 2.
This is line number 4.
[root@localhost ~]# sed '2,3d' data6.txt
This is line number 1.
This is line number 4.
[root@localhost ~]#sed '/1/,/3/d' data6.txt
#刪除第 1~3 行的文本數據
This is line number 4.
[root@localhost ~]# sed '3,$d' data6.txt
This is line number 1.
This is line number 2.
[address]a(或 i)/新文本內容
下面分別就這 2 個命令,給讀者舉幾個例子。比如說,將一個新行插入到數據流第三行前,執行命令如下:[root@localhost ~]# sed '3i/
> This is an inserted line.' data6.txt
This is line number 1.
This is line number 2.
This is an inserted line.
This is line number 3.
This is line number 4.
[root@localhost ~]# sed '3a/
> This is an appended line.' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is an appended line.
This is line number 4.
[root@localhost ~]# sed '1i/
> This is one line of new text./
> This is another line of new text.' data6.txt
This is one line of new text.
This is another line of new text.
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
[address]c/用于替換的新文本
舉個例子:[root@localhost ~]# sed '3c/
> This is a changed line of text.' data6.txt
This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.
在這個例子中,sed 編輯器會修改第三行中的文本,其實,下面的寫法也可以實現此目的:
[root@localhost ~]# sed '/number 3/c/
> This is a changed line of text.' data6.txt
This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.
[address]y/inchars/outchars/
轉換命令會對 inchars 和 outchars 值進行一對一的映射,即 inchars 中的第一個字符會被轉換為 outchars 中的第一個字符,第二個字符會被轉換成 outchars 中的第二個字符...這個映射過程會一直持續到處理完指定字符。如果 inchars 和 outchars 的長度不同,則 sed 會產生一條錯誤消息。[root@localhost ~]# sed 'y/123/789/' data8.txt
This is line number 7.
This is line number 8.
This is line number 9.
This is line number 4.
This is line number 7 again.
This is yet another line.
This is the last line in the file.
[root@localhost ~]# echo "This 1 is a test of 1 try." | sed 'y/123/456/'
This 4 is a test of 4 try.
[address]p
p 命令常見的用法是打印包含匹配文本模式的行,例如:[root@localhost ~]# cat data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
[root@localhost ~]# sed -n '/number 3/p' data6.txt
This is line number 3.
[root@localhost ~]# sed -n '/3/{
> p
> s/line/test/p
> }' data6.txt
This is line number 3.
This is test number 3.
[address]w filename
這里的 filename 表示文件名,可以使用相對路徑或絕對路徑,但不管是哪種,運行 sed 命令的用戶都必須有文件的寫權限。[root@localhost ~]# sed '1,2w test.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
[root@localhost ~]# cat test.txt
This is line number 1.
This is line number 2.
[root@localhost ~]# cat data11.txt
Blum, R Browncoat
McGuiness, A Alliance
Bresnahan, C Browncoat
Harken, C Alliance
[root@localhost ~]# sed -n '/Browncoat/w Browncoats.txt' data11.txt
cat Browncoats.txt
Blum, R Browncoat
Bresnahan, C Browncoat
[address]r filename
sed 命令會將 filename 文件中的內容插入到 address 指定行的后面,比如說:[root@localhost ~]# cat data12.txt
This is an added line.
This is the second added line.
[root@localhost ~]# sed '3r data12.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is an added line.
This is the second added line.
This is line number 4.
[root@localhost ~]# sed '$r data12.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is an added line.
This is the second added line.
[root@localhost ~]# sed '2q' test.txt
This is line number 1.
This is line number 2.
[root@localhost ~]# sed '/number 1/{ s/number 1/number 0/;q; }' test.txt
This is line number 0.
[address]腳本命令
或者
address {
多個腳本命令
}
[root@localhost ~]#sed '2s/dog/cat/' data1.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
[root@localhost ~]# sed '2,3s/dog/cat/' data1.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 cat
The quick brown fox jumps over the lazy dog
[root@localhost ~]# sed '2,$s/dog/cat/' data1.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 cat
The quick brown fox jumps over the lazy cat
/pattern/command
注意,必須用正斜線將要指定的 pattern 封起來,sed 會將該命令作用到包含指定文本模式的行上。[root@localhost ~]# grep demo /etc/passwd
demo:x:502:502::/home/Samantha:/bin/bash
[root@localhost ~]# sed '/demo/s/bash/csh/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
...
demo:x:502:502::/home/demo:/bin/csh
...
[root@localhost ~]# cat test.txt
<html>
<title>First Wed</title>
<body>
h1Helloh1
h2Helloh2
h3Helloh3
</body>
</html>
#使用正則表示式給所有第一個的h1、h2、h3添加<>,給第二個h1、h2、h3添加</>
[root@localhost ~]# cat sed.sh
/h[0-9]/{
s///<&/>/1
s///<//&/>/2
}
[root@localhost ~]# sed -f sed.sh test.txt
<h1>Hello</h1>
<h2>Hello</h2>
<h3>Hello</h3>
新聞熱點
疑難解答