sed 相關(guān)總結(jié)

一. sed 基礎(chǔ)

sed 編輯器被稱作流編輯器,它會在編輯器處理數(shù)據(jù)之前基于預(yù)先提供的一組規(guī)則來編輯數(shù)據(jù)流。在流編輯器將所有命令與一行數(shù)據(jù)匹配完畢后触幼,它會讀取下一行數(shù)據(jù)并重復(fù)這個過程衡瓶。

sed 編輯器會依次執(zhí)行如下操作:一次從輸入中讀取一行數(shù)據(jù)优质、根據(jù)所提供的編輯器命令匹配數(shù)據(jù)、按照命令修改流中的數(shù)據(jù)帚湘、將新的數(shù)據(jù)輸出到 STDOUT玫荣。

(一). sed 命令格式

sed 命令的格式如下:sed options script file。選項(xiàng)允許修改 sed 命令的行為大诸,而 script 參數(shù)指定了應(yīng)用于流數(shù)據(jù)上的單個命令捅厂。

選項(xiàng) 描述
-e script 在處理輸入時贯卦,將 script 中指定的命令添加到已有的命令中
-f file 在處理輸入時,將 file 中指定的命令添加到已有的命令中
-n 不產(chǎn)生命令輸出焙贷,使用 print 命令來完成輸出

二. sed 尋址模式

在 sed 編輯器中使用的命令默認(rèn)會作用于文本數(shù)據(jù)的所有行撵割。如果只想將命令作用于特定行或某些行,則必須用行尋址辙芍。

在 sed 編輯器中有兩種形式的行尋址:以數(shù)字形式表示行區(qū)間啡彬、用文本模式來過濾出行。這兩種形式都使用相同的格式來指定地址故硅,也可以將特定地址的多個命令分組庶灿。sed 編輯器會將指定的每條命令作用到匹配指定地址的行上。

[address]command

address {
    command1
    command2
}

(一). 數(shù)字方式的行尋址

當(dāng)使用數(shù)字方式的行尋址時吃衅,可以用行在文本流中的行位置來引用往踢。 sed 編輯器會將文本流中的第一行編號為 1,然后繼續(xù)按順序?yàn)榻酉聛淼男蟹峙湫刑柵遣恪T诿钪兄付ǖ牡刂房梢允菃蝹€行號菲语,或是用起始行號、逗號以及結(jié)尾行號指定的一定區(qū)間范圍內(nèi)的行惑灵。

$ sed '2s/dog/cat/' data.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 '2,3s/dog/cat/' data.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

$ sed '2,$s/dog/cat/' data.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

(二). 使用文本模式過濾器

sed 允許指定文本模式來過濾出命令要作用的行山上。格式如下:/pattern/command,必須用斜線將要指定的模式封起來英支。 sed 會將命令作用到匹配指定文本模式的行佩憾。可以在文本模式中使用正則表達(dá)式來創(chuàng)建匹配效果更好的模式干花。

$ cat data.txt 
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

$ sed '/daemon/s!/sbin/nologin!/bin/bash!' data.txt 
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

(三). 命令組合

如果需要在單行上執(zhí)行多條命令妄帘,可以用花括號將多條命令組合在一起。 sed 會處理地址行處列出的每條命令池凄。

$ sed '2,3{
> s/dog/cat/
> s/brown/green/
> }' data.txt
The quick brown fox jumps over the lazy dog
The quick green fox jumps over the lazy cat
The quick green fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog

三. sed 相關(guān)命令

(一). 替換命令

命令 s 會將斜線間指定的第二個文本字符串替換為第一個文本字符串模式抡驼。

$ cat data.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.

$ sed 's/dog/cat/' data.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.

1. 替換標(biāo)記

替換命令在替換多行中的文本時能正常工作,但默認(rèn)情況下它只替換每行中出現(xiàn)的第一處肿仑。要讓替換命令能夠替換一行中不同地方出現(xiàn)的文本必須使用替換標(biāo)記致盟。替換標(biāo)記在替換命令字符串之后設(shè)置。

有 4 種可用的替換標(biāo)記:

  • 數(shù)字尤慰,表明新文本將替換第幾處模式匹配的地方

  • g馏锡,表明新文本將會替換所有匹配的文本

  • p,表明原先行的內(nèi)容要打印出來

  • w file伟端,將替換的結(jié)果寫到文件中

-n 選項(xiàng)將禁止 sed 編輯器輸出杯道。但 p 替換標(biāo)記會輸出修改過的行。w 替換標(biāo)記會產(chǎn)生同樣的輸出责蝠,不過會將輸出保存到指定文件中 (只有那些包含匹配模式的行才會保存在指定的輸出文件中)党巾。

$ cat data.txt 
This is a test of the test script.
This is the second test of the test script.

$ sed 's/test/trial/2' data.txt 
This is a test of the trial script.
This is the second test of the trial script.

$ sed 's/test/trial/g' data.txt 
This is a trial of the trial script.
This is the second trial of the trial script.
cat data.txt 
This is a test line.
This is a different line.

$ sed -n 's/test/trial/p' data.txt 
This is a trial line.

$ sed 's/test/trial/w test.txt' data.txt 
This is a trial line.
This is a different line.

$ cat test.txt 
This is a trial line.

2. 替換字符

sed 編輯器允許選擇其他字符來作為替換命令中的字符串分隔符萎庭。

$ cat data.txt
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin

$ sed 's!/bin/bash!/bin/csh!' data.txt 
root:x:0:0:root:/root:/bin/csh
sync:x:5:0:sync:/sbin:/bin/sync
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin

(二). 刪除行

可以使用命令 d 刪除文本流中的特定行。它會刪除指定的尋址模式匹配的所有行齿拂。

$ cat data.txt 
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.

$ sed '2d' data.txt 
This is line number 1.
This is line number 3.
This is line number 4.

$  sed '2,3d' data.txt 
This is line number 1.
This is line number 4.

$ sed '2,$d' data.txt 
This is line number 1.

$ sed '/number 1/d' data.txt 
This is line number 2.
This is line number 3.
This is line number 4.

可以使用兩個文本模式來刪除某個區(qū)間內(nèi)的行擎椰。指定的第一個模式會 "打開" 行刪除功能,第二個模式會 "關(guān)閉" 行刪除功能创肥。 sed 會刪除兩個指定行之間的所有行 (包括指定的行)。

只要 sed 在數(shù)據(jù)流中匹配到了開始模式值朋,刪除功能就會打開叹侄。這可能會導(dǎo)致意外的結(jié)果:如果沒有找到停止模式,會將數(shù)據(jù)流中的剩余行全部刪除昨登。

$ sed '/2/,/3/d' data.txt 
This is line number 1.
This is line number 4.
$ cat data.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 1 again.
This is text you want to keep.
This is the last line in the file.

$ sed '/1/,/3/d' data7.txt
This is line number 4.

sed '/2/,/5/d' data.txt 
This is line number 1.

(三). 插入和附加文本

跟其他編輯器類似趾代,sed 編輯器允許向數(shù)據(jù)流插入和附加文本行:

  • 插入 (insert) 命令 i 會在指定行前增加一個新行

  • 附加 (append) 命令 a 會在指定行后增加一個新行

sed '[address]command\new line'
$ sed '3i\New Line' data.txt 
This is line number 1.
This is line number 2.
New Line
This is line number 3.
This is line number 4

$ sed '3a\New Line' data.txt 
This is line number 1.
This is line number 2.
This is line number 3.
New Line
This is line number 4

(四). 修改行

命令 c 允許修改數(shù)據(jù)流中整行文本的內(nèi)容。和插入和附加命令的工作機(jī)制一樣丰辣,必須在 sed 命令中單獨(dú)指定新行撒强。

sed '[address]c\new line'
$ sed '3c\Changed Line' data.txt 
This is line number 1.
This is line number 2.
Changed Line
This is line number 4

$ sed '/number 3/c\Changed Line' data.txt 
This is line number 1.
This is line number 2.
Changed Line
This is line number 4

$ sed '2,3c\Changed Line' data.txt 
This is line number 1.
Changed Line
This is line number 4

(五). 轉(zhuǎn)換命令

命令 y 是唯一處理單個字符的 sed 命令,其會對 inchars 和 outchars 進(jìn)行一對一映射笙什。在 inchars 中的第一個字符會被轉(zhuǎn)換為 outchars 中的第一個字符飘哨,第二個字符會被轉(zhuǎn)換成 outchars 中的第二個字符。如果 inchars 和 outchars 的長度不同琐凭,則會產(chǎn)生一條錯誤消息芽隆。

[address]y/inchars/outchars/
$ cat data.txt 
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4

$ sed 'y/123/789/' data.txt 
This is line number 7.
This is line number 8.
This is line number 9.
This is line number 4

(六). 打印命令

命令 p 用來打印文本行。命令 = 用來打印行號统屈。命令l (小寫的 L) 用來列出行胚吁。

1. 打印行

跟替換命令中的 p 標(biāo)記類似,命令 p 可以打印 sed 輸出中的一行愁憔。在命令行上用 -n 選項(xiàng)腕扶,可以禁止輸出其他行,只打印包含匹配文本模式的行吨掌。

$ sed -n '/number 3/p' data.txt 
This is line number 3.

如果需要在修改之前查看行半抱,也可以使用打印命令,比如與替換或修改命令一起使用膜宋〈海可以創(chuàng)建一個腳本在修改行之前顯示該行。

$ sed -n '/number 3/{p; s/line/test/p}' data.txt 
This is line number 3.
This is test number 3.

2. 打印行號

命令 = 會打印行在數(shù)據(jù)流中的當(dāng)前行號激蹲。行號由數(shù)據(jù)流中的換行符決定棉磨。每次數(shù)據(jù)流中出現(xiàn)一個換行符,sed 編輯器會認(rèn)為一行文本結(jié)束学辱。

$ sed '=' data.txt 
1
The quick brown fox jumps over the lazy dog.
2
The quick brown fox jumps over the lazy dog.
3
The quick brown fox jumps over the lazy dog.
4
The quick brown fox jumps over the lazy dog.
$ sed -n '/number 4/{=;p}' data.txt 
4
This is line number 4

3. 列出行

列出 (list) 命令 l 可以打印數(shù)據(jù)流中的文本和不可打印的 ASCII 字符乘瓤。任何不可打印字符要么在其八進(jìn)制值前加一個反斜線环形,要么使用標(biāo)準(zhǔn) C 風(fēng)格的命名法。制表符的位置使用 \t 來顯示衙傀。行尾的美元符表示換行符抬吟。如果數(shù)據(jù)流包含了轉(zhuǎn)義字符,列出命令會在必要時候用八進(jìn)制碼來顯示统抬。

$ cat data.txt 
This    line    contains    tabs.

$ sed -n 'l' data.txt 
This\tline\tcontains\ttabs.$

(七). 處理文件

1. 寫入文件

使用 w 命令用來向文件寫入行火本。格式如下:[address]w filename。filename 可以使用相對路徑或絕對路徑聪建,運(yùn)行 sed 編輯器的用戶都必須有文件的寫權(quán)限钙畔。如果不想讓行顯示到 STDOUT 上,可以用 sed 命令的 -n 選項(xiàng)金麸。

$ sed '1,2w test.txt' data.txt 
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.

$ cat test.txt 
This is line number 1.
This is line number 2.

2. 從文件讀取數(shù)據(jù)

讀取命令 r 允許將一個獨(dú)立文件中的數(shù)據(jù)插入到數(shù)據(jù)流擎析。filename 參數(shù)指定了數(shù)據(jù)文件的絕對路徑或相對路徑。在讀取命令中使用地址區(qū)間挥下,只能指定單獨(dú)一個行號或文本模式地址揍魂。 sed 會將文件中的文本插到指定地址后。讀取命令的格式如下:

[address]r filename
$ cat test.txt 
This is an added line.
This is the second added line.

$ sed '3r test.txt' data.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.

讀取命令的另一個很酷的用法是和刪除命令配合使用:利用另一個文件中的數(shù)據(jù)來替換文件中的占位文本棚瘟。

$ cat test.txt 
This is an added line.
This is the second added line.

$ sed '/number 3/{
r test.txt
d
}' data.txt
This is line number 1.
This is line number 2.
This is an added line.
This is the second added line.
This is line number 4.

四. sed 進(jìn)階

(一). 多行命令

sed 編輯器包含了 3 個可用來處理多行文本的特殊命令:

  • N:將數(shù)據(jù)流中的下一行加進(jìn)來創(chuàng)建一個多行組來處理

  • D:刪除多行組中的一行

  • P:打印多行組中的一行

1. next 命令

單行的 next 命令

小寫的 n 命令會告訴 sed 移動到數(shù)據(jù)流中的下一文本行现斋,而不用重新回到命令的最開始再執(zhí)行一遍。通常 sed 在移動到數(shù)據(jù)流中的下一文本行之前偎蘸,會在當(dāng)前行上執(zhí)行完所有定義好的命令步责,單行 next 命令改變了這個流程。

$ cat data.txt 
This is the header line.

This is a data line.

This is the last line.
$
$ sed '/^$/d' data.txt 
This is the header line.
This is a data line.
This is the last line.
$
$ sed '/header/{n;d}' data.txt 
This is the header line.
This is a data line.

This is the last line.

合并文本行

多行版本的 next 命令 N 會將下一文本行添加到模式空間中已有的文本后禀苦,文本行仍然用換行符分隔蔓肯。作用是將數(shù)據(jù)流中的兩個文本行合并到同一個模式空間中。

$ cat data.txt 
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.

$ sed '/first/{N;s/\n/ /}' data.txt 
This is the header line.
This is the first data line. This is the second data line.
This is the last line.

sed '/first/{N;N;s/\n//}' data.txt 
This is the header line.
This is the first data line.This is the second data line.
This is the last line.

如果要在數(shù)據(jù)文件中查找一個可能會分散在兩行中的文本短語的話振乏,這是個很實(shí)用的應(yīng)用程序蔗包。

$ cat data.txt 
On Tuesday, the Linux System
Administrator's group meeting will be held.
All System Administrators should attend.

$ sed '{N
s/System Administrator/Desktop User/
s/System\nAdministrator/Desktop\nUser/
}' data.txt 
On Tuesday, the Linux Desktop
User's group meeting will be held.
All System Administrators should attend.

$ sed '{
s/System Administrator/Desktop User/
N
s/System\nAdministrator/Desktop\nUser/
}' data.txt 
On Tuesday, the Linux Desktop
User's group meeting will be held.
All Desktop Users should attend.

2. 多行刪除命令

命令 d 用來刪除模式空間中的當(dāng)前行。但和命令 N 一起使用時慧邮,它會將模式空間中匹配的兩行都刪掉调限。

sed 提供了多行刪除命令 D,它只刪除模式空間中的第一行误澳。該命令會刪除到換行符 (含換行符) 為止的所有字符耻矮。

$ cat data.txt 
On Tuesday, the Linux System
Administrator's group meeting will be held.
All System Administrators should attend.

$ sed '{N;/System\nAdministrator/d}' data.txt 
All System Administrators should attend.

$ sed '{N;/System\nAdministrator/D}' data.txt 
Administrator's group meeting will be held.
All System Administrators should attend.

3. 多行打印命令

命令 P 只打印多行模式空間中的第一行,這包括模式空間中直到換行符為止的所有字符忆谓。用 -n 選項(xiàng)來阻止腳本輸出時裆装,它和顯示文本的單行 p 命令的用法大同小異。

$ cat data.txt 
On Tuesday, the Linux System
Administrator's group meeting will be held.
All System Administrators should attend.

$ sed -n '{N;/System\nAdministrator/P}' data.txt 
On Tuesday, the Linux System

二. 保持空間

模式空間是一塊活躍的緩沖區(qū),在 sed 執(zhí)行命令時它會保存待檢查的文本哨免。但它并不是 sed 保存文本的唯一空間茎活。sed 有另一塊稱作保持空間的緩沖區(qū)域。在處理模式空間中的某些行時琢唾,可以用保持空間來臨時保存一些行载荔。有 5 條命令可用來操作保持空間。

命令 描述
h 將模式空間復(fù)制到保持空間
H 將模式空間附加到保持空間
g 將保持空間復(fù)制到模式空間
G 將保持空間附加到模式空間
x 交換模式空間和保持空間的內(nèi)容
$ cat data.txt 
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.

$ sed -n '/first/ {h ; p ; n ; p ; g ; p }' data.txt 
This is the first data line.
This is the second data line.
This is the first data line.

三. 排除命令

感嘆號命令 (!) 用來排除命令采桃,讓原本會起作用的命令不起作用懒熙。

$ cat data.txt 
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.

$ sed -n '/first/!p' data.txt 
This is the header line.
This is the second data line.
This is the last line.

四. 改變流

sed 會從腳本的頂部開始,一直執(zhí)行到腳本的結(jié)尾 (D 命令例外)普办。 sed 提供了一個方法來改變命令腳本的執(zhí)行流程工扎,其結(jié)果與結(jié)構(gòu)化編程類似。

1. 分支

sed 提供了一種方法泌豆,可以基于地址、地址模式或地址區(qū)間排除一整塊命令吏饿。這允許只對數(shù)據(jù)流中的特定行執(zhí)行一組命令踪危。分支命令 b 的格式如下:

[address]b [label]

address 參數(shù)決定了哪些行的數(shù)據(jù)會觸發(fā)分支命令。label 參數(shù)定義了要跳轉(zhuǎn)到的位置猪落。如果沒有加 label 參數(shù)贞远,跳轉(zhuǎn)命令會跳轉(zhuǎn)到腳本的結(jié)尾。

$ cat data.txt 
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.
$
$ sed '{2,3b;s/This is/Is this/;s/line/line?/}' data.txt 
Is this the header line?.
This is the first data line.
This is the second data line.
Is this the last line?.

要是不想直接跳到腳本的結(jié)尾笨忌,可以為分支命令定義一個要跳轉(zhuǎn)到的標(biāo)簽蓝仲。標(biāo)簽以冒號開始,最多 7 個字符長度官疲。將指定的標(biāo)簽加到 b 命令后袱结。使用標(biāo)簽允許跳過地址匹配處的命令,但仍然執(zhí)行腳本中的其他命令途凫。

$ sed '{
/first/b jump1
s/This is the/jump here on/
:jump1 s/This is the/no jump on/
}' data.txt 
jump here on header line.
no jump on first data line.
jump here on second data line.
jump here on last line.`

$ echo "This, is, a, test, to, remove, commas." | sed -n '{
:start
s/,//1p
/,/b start
}' 
This is, a, test, to, remove, commas.
This is a, test, to, remove, commas.
This is a test, to, remove, commas.
This is a test to, remove, commas.
This is a test to remove, commas.
This is a test to remove commas.

2. 測試

測試命令 t 可以用來改變 sed 腳本的執(zhí)行流程垢夹。測試命令會根據(jù)替換命令的結(jié)果跳轉(zhuǎn)到某個標(biāo)簽,而不是根據(jù)地址進(jìn)行跳轉(zhuǎn)维费。如果替換命令成功匹配并替換了一個模式果元,測試命令就會跳轉(zhuǎn)到指定的標(biāo)簽。如果替換命令未能匹配指定的模式犀盟,測試命令就不會跳轉(zhuǎn)而晒。未指定標(biāo)簽的情況下,如果測試成功阅畴,sed 會跳轉(zhuǎn)到腳本的結(jié)尾倡怎。

[address]t [label]
$ cat data.txt 
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.
$
sed '{
> s/first/matched/
> t
> s/This is the/No match on/
> }' data.txt
No match on header line.
This is the matched data line.
No match on second data line.
No match on last line.

五. 模式替代

1. & 符號

& 符號可以用來代表替換命令中的匹配的模式。不管模式匹配到的是什么樣的文本,都可以在替代模式中使用 & 符號來使用這段文本诈胜。

$ echo "The cat sleep in his hat." | sed 's/.at/".at"/g' 
The ".at" sleep in his ".at".

$ echo "The cat sleep in his hat." | sed 's/.at/"&"/g' 
The "cat" sleep in his "hat".

2. 替代單獨(dú)的單詞

& 符號會提取匹配替換命令中指定模式的整個字符串豹障,但有的時候只想提取這個字符串的一部分。sed 用圓括號來定義替換模式中的子模式焦匈⊙可以在替代模式中使用特殊字符來引用每個子模式。替代字符由反斜線和數(shù)字組成缓熟。數(shù)字表明子模式的位置累魔。sed 會給第一個子模式分配字符 \1,給第二個子模式分配字符 \2够滑,依此類推垦写。

當(dāng)在替換命令中使用圓括號時,必須用轉(zhuǎn)義字符將它們標(biāo)示為分組字符而不是普通的圓括號彰触。這跟轉(zhuǎn)義其他特殊字符正好相反梯投。

$ echo "The System Administrator manual" | sed 's/\(System\) Administrator/\1 User/'
The System User manual
$ echo "1234567891" | sed '{
:start
s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/
t start
}'
1,234,567,891

六. 創(chuàng)建 sed 實(shí)用工具

1. 加倍行間距

$ cat data.txt 
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.

$ sed '$!G' data.txt 
This is the header line.

This is the first data line.

This is the second data line.

This is the last line.

2. 對可能含有空白行的文件加倍行間距

$ cat data.txt 
This is the header line.

This is the first data line.


This is the second data line.
This is the last line.
$
$ sed '{/^$/d;$!G}' data.txt 
This is the header line.

This is the first data line.

This is the second data line.

This is the last line.

3. 給文件中的行編號

$ cat data.txt 
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.

$ sed '=' data.txt | sed '{N;s/\n/ /}'
1 This is the header line.
2 This is the first data line.
3 This is the second data line.
4 This is the last line.

4. 打印末尾行

$ cat data.txt 
This is line 1.
This is line 2.
This is line 3.
This is line 4.
This is line 5.
This is line 6.
This is line 7.
This is line 8.
This is line 9.
This is line 10.
This is line 11.
This is line 12.
This is line 13.
This is line 14.
This is line 15.

$ sed ':start;{$q;N;11,$D};b start' data.txt 
This is line 6.
This is line 7.
This is line 8.
This is line 9.
This is line 10.
This is line 11.
This is line 12.
This is line 13.
This is line 14.
This is line 15.

5. 刪除行

刪除連續(xù)的空白行

刪除連續(xù)空白行的最簡單辦法是用地址區(qū)間來檢查數(shù)據(jù)流。sed 會對所有匹配指定地址區(qū)間的行執(zhí)行該命令况毅。刪除連續(xù)空白行的關(guān)鍵在于創(chuàng)建包含一個非空白行和一個空白行的地址區(qū)間分蓖。如果 sed 遇到了這個區(qū)間,它不會刪除行尔许。但對于不匹配這個區(qū)間的行么鹤,它會刪除這些行。

$ cat data.txt 
This is line one.


This is line two.

This is line three.



This is line four.
$
$ sed '/./,/^$/!d' data.txt 
This is line one.

This is line two.

This is line three.

This is line four.

刪除開頭的空白行

$ cat data.txt 



This is line one.

This is line two.
$
$ sed '/./,$!d' data.txt 
This is line one.

This is line two.

6. 刪除 HTML 標(biāo)簽

$ cat data.txt 
<html>
<head>
<title>This is the page title</title>
</head>
<body>
<p>
This is the <b>first</b> line in the Web page.
This should provide some <i>useful</i>
information to use in our sed script.
</body>
</html>

$ sed 's/<[^>]*>//g;/^$/d' data.txt 
This is the page title
This is the first line in the Web page.
This should provide some useful
information to use in our sed script.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末味廊,一起剝皮案震驚了整個濱河市蒸甜,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌余佛,老刑警劉巖柠新,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異辉巡,居然都是意外死亡登颓,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進(jìn)店門红氯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來框咙,“玉大人,你說我怎么就攤上這事痢甘±觯” “怎么了?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵塞栅,是天一觀的道長者铜。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么作烟? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任愉粤,我火速辦了婚禮,結(jié)果婚禮上拿撩,老公的妹妹穿的比我還像新娘衣厘。我一直安慰自己,他們只是感情好压恒,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布影暴。 她就那樣靜靜地躺著,像睡著了一般探赫。 火紅的嫁衣襯著肌膚如雪型宙。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天伦吠,我揣著相機(jī)與錄音妆兑,去河邊找鬼。 笑死毛仪,一個胖子當(dāng)著我的面吹牛搁嗓,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播潭千,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼谱姓,長吁一口氣:“原來是場噩夢啊……” “哼借尿!你這毒婦竟也來了刨晴?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤路翻,失蹤者是張志新(化名)和其女友劉穎狈癞,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體茂契,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蝶桶,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了掉冶。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片真竖。...
    茶點(diǎn)故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖厌小,靈堂內(nèi)的尸體忽然破棺而出恢共,到底是詐尸還是另有隱情,我是刑警寧澤璧亚,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布讨韭,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏透硝。R本人自食惡果不足惜狰闪,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望濒生。 院中可真熱鬧埋泵,春花似錦、人聲如沸甜攀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽规阀。三九已至恒序,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間谁撼,已是汗流浹背歧胁。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留厉碟,地道東北人喊巍。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像箍鼓,于是被迫代替她去往敵國和親崭参。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評論 2 361

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

  • linux資料總章2.1 1.0寫的不好抱歉 但是2.0已經(jīng)改了很多 但是錯誤還是無法避免 以后資料會慢慢更新 大...
    數(shù)據(jù)革命閱讀 12,176評論 2 33
  • 本文承接之前寫的三十分鐘學(xué)會AWK一文款咖,在學(xué)習(xí)完AWK之后何暮,趁熱打鐵又學(xué)習(xí)了一下SED,不得不說這兩個工具真的堪稱...
    mylxsw閱讀 4,401評論 3 74
  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,408評論 0 5
  • 一铐殃、前言 我們都知道海洼,在Linux中一切皆文件,比如配置文件富腊,日志文件坏逢,啟動文件等等。如果我們相對這些文件進(jìn)行一些...
    以七v為書閱讀 1,524評論 0 5
  • 這次的作品是海景赘被,現(xiàn)在正值隆冬是整,白雪皚皚的季節(jié),你是否想念碧海藍(lán)天民假? 一開始畫云浮入,一筆水多了,差點(diǎn)讓我重畫阳欲!想想這...
    半糖洛洛閱讀 301評論 1 9