鍍金池/ 教程/ Linux/ Sed基本命令
Sed字符串
Sed實(shí)用功能
Sed管理模式
sed環(huán)境設(shè)置
sed模式緩沖區(qū)
Sed循環(huán)
Sed模式范圍
Sed教程
Sed正則表達(dá)式
Sed分支
Sed基本語法
sed工作流程
Sed特殊字符
Sed基本命令

Sed基本命令

本教程將介紹一些有用的sed命令和使用示例??紤]一下我們有一個(gè)文本文件books.txt待處理,它有以下內(nèi)容:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

刪除 d 命令

sed刪除命令用 d 字符表示,并將其用于刪除從一個(gè)給定的模式緩沖器的一行或多行。以下是 sed 刪除命令的基本語法:

[address1[,address2]]d

這里address1和address2分別為起始和結(jié)束地址,其可以是行號(hào)或模式串。這兩個(gè)地址是可選參數(shù),如果不提供它們作為前綴-d命令,那么它會(huì)刪除,如下圖所示所有行:

[jerry]$ sed 'd' books.txt 

上面的命令將刪除所有傳遞給 sed 的行并且沒有行數(shù)據(jù)會(huì)打印在屏幕上。

下面指示 sed 只在某些行上使用。下面的例子中只刪除4行。

[jerry]$ sed '4d' books.txt 

執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

此外,sed也接受用逗號(hào)分隔地址范圍(,)??梢灾甘緎ed 刪除N1到N2行。例如,下面的例子將刪除從2到4的所有行。

[jerry]$ sed '2,4d' books.txt 

執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:

1) A Storm of Swords, George R. R. Martin, 1216 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

也可以指定模式作為地址。下面的示例刪除作者是 Paulo Coelho的所有書籍。

[jerry]$ sed '/Paulo Coelho/d' books.txt 

執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
6) A Game of Thrones, George R. R. Martin, 864 

也可以使用文本模式指定一個(gè)地址范圍。下面的示例刪除模式Storm 和Fellowship之間的所有行。

[jerry]$ sed '/Storm/,/Fellowship/d' books.txt  
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 
可以使用美元符號(hào)($),加號(hào)(+),和波浪符號(hào)(?)運(yùn)算符使用sed -d命令刪除。

寫入w 命令

sed的寫命令是由 w 字符表示,并且它用于存儲(chǔ)模式緩沖區(qū)的一個(gè)文件中內(nèi)容。以下是sed 寫命令的基本語法:

[address1[,address2]]w 

這里,address1 和 address2 分別是模式緩沖存儲(chǔ)器的起始和結(jié)束地址,該地址可以是行號(hào)或模式串。這兩個(gè)地址是可選參數(shù),如果不提供它們的前綴給w命令,那么它將存儲(chǔ)完整的模式緩沖區(qū)到給定的文件,如下所示:

[jerry]$ sed -n 'w books.bak' books.txt 

上面的命令將創(chuàng)建另一個(gè)名為books.bak的文件。這是books.txt文件復(fù)制文件。

sed允許創(chuàng)建包含源文件只有某些行的文件。以下命令是副本只從books.txt偶數(shù)行數(shù)據(jù)到books.bak文件。

[jerry]$ sed -n '2~2w books.bak' books.txt  

如果將檢查books.bak文件的內(nèi)容,那么它將有以下幾行:

2) The Two Towers, J. R. R. Tolkien, 352 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
6) A Game of Thrones, George R. R. Martin, 864 

也可以指定模式作為地址。下面的例子中存儲(chǔ)作者為 Paulo Coelho 的所有書籍。

[jerry]$ sed -n -e '/Paulo Coelho/w books.bak' books.txt

如果將檢查books.bak文件的內(nèi)容,那么它將有以下幾行:

3) The Alchemist, Paulo Coelho, 197
5) The Pilgrimage, Paulo Coelho, 288

追加 a 命令

任何一個(gè)文本編輯器的最有用的操作是提供附/追加功能。sed通過其由一個(gè)字符表示追加命令支持該操作。以下是sed追加命令的基本語法:

[address]a  'text to be appended'

這里的地址是模式緩沖區(qū)地址,可以是行號(hào)或模式字符串來表示,其中的文本將被追加的位置。以下是追加后的行數(shù)4新書項(xiàng)命令。

[jerry]$ sed '4a 7) Adultry, Paulo Coelho, 234' books.txt 

執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
7) Adultry, Paulo Coelho, 234 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

可以使用$符號(hào)插入的文件結(jié)束后面的行,如下所示:

[jerry]$ sed '$a 7) Adultry, Paulo Coelho, 234' books.txt

執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:

1) A Storm of Swords, George R. R. Martin, 1216
2) The Two Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo Coelho, 197
4) The Fellowship of the Ring, J. R. R. Tolkien, 432
5) The Pilgrimage, Paulo Coelho, 288
6) A Game of Thrones, George R. R. Martin, 864
7) Adultry, Paulo Coelho, 234

除了行數(shù),還可以使用文本模式指定一個(gè)地址。例如,下面的例子匹配字符串后追加文本The Alchemist.

[jerry]$ sed '/The Alchemist/a 7) Adultry, Paulo Coelho, 234' books.txt  

執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
7) Adultry, Paulo Coelho, 234 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 

修改 c 命令

sed提供更改或更換用c字符來表示命令。此命令可以幫助更換新文本的現(xiàn)有行。以下是 sed 改變命令的基本語法:

[address1[,address2]]c 'Next text'

這里,address1 和 address2 分別是模式緩沖區(qū)的起始和結(jié)束地址,該地址可以是行號(hào)或模式串。這兩個(gè)地址是可選參數(shù),如果不提供前綴,則該命令將替換為新文本的每一行,如下所示:

[jerry]$ sed 'c This is new text' books.txt 

執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:

This is new text
This is new text
This is new text
This is new text
This is new text
This is new text

下面是示例替換一些其他文本的第三行。

[jerry]$ sed '3 c 3) Adultry, Paulo Coelho, 324' books.txt

執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) Adultry, Paulo Coelho, 324 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

還可以指定要匹配并采用c運(yùn)算符的幫助下替換模式如下:

[jerry]$ sed '/The Alchemist/c 3) Adultry, Paulo Coelho, 324' books.txt

執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) Adultry, Paulo Coelho, 324 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864 

sed 還允許替換多行以及一行。下面的示例是從第4行到第6行,將它們替換為新的文本。

[jerry]$ sed '4, 6c 4) Adultry, Paulo Coelho, 324' books.txt  

執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
4) Adultry, Paulo Coelho, 324

插入 i 命令

插入命令工作起來作為追加的方式相同。唯一的區(qū)別在于,它插入一個(gè)特定位置之前的行。以下是sed的插入命令的基本語法:

[address]i 'Text to be inserted' 

這里地址是模式緩沖區(qū)地址,可以用行號(hào)或模式串來表示,其中的文本將被插入的位置。下面是插入第4行之前的一本新書項(xiàng)命令。

[jerry]$ sed '4 i 7) Adultry, Paulo Coelho, 324' books.txt 

執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) The Alchemist, Paulo Coelho, 197 
7) Adultry, Paulo Coelho, 324 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

在一個(gè)文件的開頭插入文本,提供的行地址為1.下列命令說明這一點(diǎn):

[jerry]$ sed '1 i 7) Adultry, Paulo Coelho, 324' books.txt

執(zhí)行上面的代碼,會(huì)得到如下結(jié)果:

上一篇:Sed管理模式