Linux 中【sed】 命令快速參考
目錄
一、sed 命令簡(jiǎn)介
輸入流
輸出流
二、sed 命令格式
常用選項(xiàng)
常用命令
正則表達(dá)式
三、使用舉例
內(nèi)容顯示
內(nèi)容替換
內(nèi)容添加
行刪除
行替換
內(nèi)容搜尋
級(jí)聯(lián)操作

GNU 官方文檔
一、sed 命令簡(jiǎn)介
sed命令的全稱(chēng)是:stream editor(數(shù)據(jù)流編輯器),言下之意就是對(duì)流式數(shù)據(jù)進(jìn)行處理。
主要使用場(chǎng)景:對(duì)文本中的特定字符串進(jìn)行替換或者刪除。
它每次從輸入流中讀取一行內(nèi)容,去掉末尾的換行符之后,放在內(nèi)部的一個(gè)緩沖區(qū)中,
然后根據(jù)命令參數(shù)對(duì)緩沖區(qū)中數(shù)據(jù)進(jìn)行處理,最后把處理后數(shù)據(jù)輸出到輸出流中。
輸入流可以是:終端輸入內(nèi)容、管道、文件;
輸出流可以是:終端輸出、文件。
1. 輸入流:終端輸入
$ sed 's/my/your/g' -
這條指令意思是:把輸入字符中的字符串 "my" 替換成 "your"。
最后的 - 表示sed命令將會(huì)讀取用戶(hù)的輸入內(nèi)容,用戶(hù)輸入完一行內(nèi)容(按下回車(chē)):
this is my book
此時(shí),sed就會(huì)接收到這行內(nèi)容,然后進(jìn)行處理,最后把處理結(jié)果輸出到終端屏幕上:
this is your book
當(dāng)處理完這行內(nèi)容之后,繼續(xù)等待讀取用戶(hù)的下一行輸入內(nèi)容。
2. 輸入流:管道
$ echo "this is my book" | sed 's/my/your/g'
this is your book
echo 命令的輸出結(jié)果,經(jīng)過(guò)管道被sed命令讀取,然后進(jìn)行處理,最后輸出到終端屏幕上。
3. 輸入流:文件
測(cè)試文件file.txt中內(nèi)容如下:
This is my book
This is my box
把文件中字符串my替換成your。
$ sed 's/my/his/g' file.txt
This is your book
This is your box
上面這條指令把處理結(jié)果輸出到終端屏幕上,對(duì)file.txt文件本身并沒(méi)有任何改動(dòng)。
如果想直接對(duì)文件進(jìn)行操作,那么就需要加上一個(gè)選項(xiàng):-i,完整指令如下:
$ sed -i 's/my/his/g' file.txt
4. 輸出流
上面的幾個(gè)示例中, sed命令直接把輸出流(處理結(jié)果)輸出到終端屏幕上。
除此之外,sed還可以把輸出流通過(guò)管道傳遞給其它指令,例如:
$ echo "this is my book" | sed 's/my/your/g' | wc
1 4 18
還可以讓sed把處理結(jié)果重導(dǎo)向到一個(gè)文件中:
$ echo "this is my book" | sed 's/my/your/g' > test.txt
$ cat test.txt
this is your book
或者是直接在指令中通過(guò) w標(biāo)志指定輸出文件:
$ echo "this is my book" | sed 's/my/your/gw test.txt'
$ cat test.txt
this is your book
二、sed 命令格式
$ sed [選項(xiàng)] [命令] [輸入文件]
常用選項(xiàng):
-i: 直接對(duì)文件內(nèi)容進(jìn)行修改;
-e: 直接在指令列模式上進(jìn)行動(dòng)作編輯;
-f: 執(zhí)行一個(gè)文件內(nèi)的 sed 命令;
-n: 使用安靜模式;
-r: 使用擴(kuò)展的正則表達(dá)式來(lái)進(jìn)行內(nèi)容匹配;
常用命令:
a: 在當(dāng)前選擇行的后面追加一行;
i: 在當(dāng)前選擇行的前面追加一行;
c: 把當(dāng)前選擇行內(nèi)容進(jìn)行替換;
d: 刪除當(dāng)前選擇的行;
s: 對(duì)當(dāng)前行中選擇的字符串進(jìn)行替換;
g: 在當(dāng)前選擇行中進(jìn)行全部替換;
p: 打印選擇的行;
支持的正則表達(dá)式:
^: 行首定位符;
$: 行尾定位符;
.: 匹配除了換行符之外的其他任意一個(gè)字符;
+: 匹配前面一個(gè)字符重復(fù) 1 次或多次;
*: 匹配前面一個(gè)字符重復(fù) 0 次或多次;
{i}: 匹配前面一個(gè)字符重復(fù) i 次;
{i,}: 匹配前面一個(gè)字符重復(fù)至少 i 次;
{i,j}: 匹配前面一個(gè)字符重復(fù) i ~ j 次;
\n: 匹配換行符
三、使用舉例
為了便于演示,以下示例中的輸入流來(lái)自文件,輸出到終端屏幕上。
測(cè)試文件file.txt內(nèi)容:
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!
如果需要直接對(duì)文件進(jìn)行修改的話,加上
-i選項(xiàng)即可。
內(nèi)容顯示
示例:顯示文件中第2行:
$ sed -n '2p' file.txt
line2: That is your book, a nice book.
示例:顯示文件中第2 ~ 4行:
$ sed -n '2,4p' file.txt
line2: That is your book, a nice book.
line3: I am 30 years old.
line4: You are 30 years old.
示例:顯示文件中除了2 ~ 4行之外的其它行:
$ sed '2,4d' file.txt
line1: This is my book, a nice book.
line5: Welcome to www.linuxbar.net!
內(nèi)容替換
示例:把每一行中的book替換成picture:
$ sed 's/book/picture/' file.txt
line1: This is my picture, a nice book.
line2: That is your picture, a nice book.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!
可以看到:第1、2行中只有第一個(gè)book字符串被替換了,后面的那個(gè)沒(méi)有被替換。
示例:如果要全部替換,需要加上g標(biāo)志,如下:
$ sed 's/book/picture/g' file.txt
line1: This is my picture, a nice picture.
line2: That is your picture, a nice picture.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!
內(nèi)容替換的格式是:sed 's/old/new/g' flie,使用斜杠/進(jìn)行分割,在處理文件路徑時(shí)就顯得很麻煩,例如:
示例:把路徑分隔符進(jìn)行替換:
$ echo "/home/tmp" | sed 's/\//\\/g'
\home\tmp
此時(shí),可以使用除了換行符之外的其他任意字符來(lái)替代,例如:
$ echo "/home/tmp" | sed 's#/#\\#g'
\home\tmp
\:因?yàn)榉葱备茏陨硎且粋€(gè)特殊的字符,所以需要進(jìn)行轉(zhuǎn)義。
另外一個(gè)問(wèn)題:sed把每一行都輸出到屏幕上了,包括沒(méi)有匹配的那些行。
如何只顯示處理的那些行呢?
示例:把每一行中的book替換成picture: ,但是只顯示處理的行:
$ sed -n 's/book/picture/gp' file.txt
line1: This is my picture, a nice picture.
line2: That is your picture, a nice picture.
示例:將每一行中的第2個(gè)book替換成picture:
$ $ sed -n 's/book/picture/2p' file.txt
line1: This is my book, a nice picture.
line2: That is your book, a nice picture.
示例:將2 ~ 3行中的book替換成picture:
$ sed '2,3s/book/picture/g' file.txt
line1: This is my book, a nice book.
line2: That is your picture, a nice picture.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!
示例:將 2 ~ 最后一行中的30替換成100:
$ sed '2,$s/30/100/g' file.txt
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 100 years old.
line4: You are 100 years old.
line5: Welcome to www.linuxbar.net!
示例:找出包含字符years的那些行,將這些行中的30替換成100:
$ sed '/years/s/30/100/g' file.txt
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 100 years old.
line4: You are 100 years old.
line5: Welcome to www.linuxbar.net!
示例:找出滿(mǎn)足 Th[is|at] 模式的那些行,將這些行中的book替換成picture:
$ sed '/Th[is|at]/s/book/picture/g' file.txt
line1: This is my picture, a nice picture.
line2: That is your picture, a nice picture.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!
示例:找出包含www 和 net的那些行,把bar替換成BAR:
$ sed '/www/,/net/ s/bar/BAR/' file.txt
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxBAR.net!
示例:忽略大小寫(xiě),把每行中的 OLD 替換成###:
$ sed 's/OLD/###/i' file.txt
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 30 years ###.
line4: You are 30 years ###.
line5: Welcome to www.linuxbar.net!
示例:從第2行開(kāi)始一直到包含am行之間的所有行中,把30替換成100:
$ sed '2,/am/s/30/100/' file.txt
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 100 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!
示例:從包含am開(kāi)始的那行開(kāi)始一直到最后一行,把其中的數(shù)字替換成#:
$ sed '/am/,$ s/[0-9]/#/g' file.txt
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line#: I am ## years old.
line#: You are ## years old.
line#: Welcome to www.linuxbar.net!
示例:把每一行中的小寫(xiě)字母a替換成大寫(xiě)字符A, 小寫(xiě)字母b替換成大寫(xiě)字符B:
$ sed 'y/ab/AB/' file.txt
line1: This is my Book, A nice Book.
line2: ThAt is your Book, A nice Book.
line3: I Am 30 yeArs old.
line4: You Are 30 yeArs old.
line5: Welcome to www.linuxBAr.net!
內(nèi)容添加
示例:在第2行的前面添加一行內(nèi)容:
$ sed '2i\new line: hello' file.txt
line1: This is my book, a nice book.
new line: hello
line2: That is your book, a nice book.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!
示例:在第2行后面面添加一行內(nèi)容:
$ sed '2a\new line: hello' file.txt
line1: This is my book, a nice book.
line2: That is your book, a nice book.
new line: hello
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!
行刪除
示例:刪除2 ~ 4行:
$ sed '2,4d' file.txt
line1: This is my book, a nice book.
line5: Welcome to www.linuxbar.net!
示例:刪除包含字符串book的行:
$ sed '/book/d' file.txt
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!
示例:刪除不包含字符串book的行:
$ sed '/book/!d' file.txt
line1: This is my book, a nice book.
line2: That is your book, a nice book.
示例:刪除空白行,包括空格
$ sed '/^\s*$/d' file.txt
line1: This is my book, a nice book.
line2: That is your book, a nice book.
line3: I am 30 years old.
line4: You are 30 years old.
line5: Welcome to www.linuxbar.net!
行替換
示例:把2 ~ 4行替換成delete:
$ sed '2,4 cdelete' file.txt
line1: This is my book, a nice book.
delete
line5: Welcome to www.linuxbar.net!
內(nèi)容搜尋
示例:查找哪些行包含字符串old:
$ sed -n '/old/=' file.txt
3
4
示例:統(tǒng)計(jì)文件中一共有多少行:
$ sed -n '$=' file.txt
5
級(jí)聯(lián)操作
示例:把2 ~ 4行內(nèi)容中的book替換成picture,30替換成100:
$ sed -n '2,4p' file.txt | sed 's/book/picture/g' | sed 's/30/100/g'
line2: That is your picture, a nice picture.
line3: I am 100 years old.
line4: You are 100 years old.
示例:把2 ~ 4行內(nèi)容中的book替換成picture,30替換成100:
$ sed -n '2,4p' file.txt | sed -e 's/book/picture/g' -e 's/30/100/g'
line2: That is your picture, a nice picture.
line3: I am 100 years old.
line4: You are 100 years old.
