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

Sed特殊字符

Sed提供了被當(dāng)作命令兩個特殊字符。本章說明了這兩個特殊字符的使用。嘗試使用這些命令,考慮有一個文本文件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

= 命令

=命令將行號后跟其標(biāo)準(zhǔn)輸出流的內(nèi)容。下面給出的是=命令的語法:

[address1[,address2]]=

這里address1 和 address2分別為起始和結(jié)束地址,其可以是行號或模式串。這兩個地址是可選參數(shù),如果不提供它們作為前綴=命令,如下圖所示,將打印的所有行的行號:

[jerry]$ sed '=' books.txt 

當(dāng)執(zhí)行上面的代碼,會得到如下結(jié)果:

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

下面的命令打印首4行的行號和剩余不帶行號:

[jerry]$ sed '1,4=' books.txt 

當(dāng)執(zhí)行上面的代碼,會得到如下結(jié)果:

1
1) A Storm of Swords, George R. R. Martin, 1216
2
2) The Two Towers, J. R. R. Tolkien, 352
3
3) The Alchemist, Paulo Coelho, 197
4
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

下面的例子打印包含模式“Paulo”的行號。

[jerry]$ sed '/Paulo/=' books.txt 

當(dāng)執(zhí)行上面的代碼,會得到如下結(jié)果:

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

下面的例子將打印僅在最后一行的行號:

[jerry]$ sed '$=' books.txt

當(dāng)執(zhí)行上面的代碼,會得到如下結(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
6) A Game of Thrones, George R. R. Martin, 864

下面的例子僅打印對所有行的行號:

[jerry]$ sed -n '='  books.txt

當(dāng)執(zhí)行上面的代碼,會得到如下結(jié)果:

1
2
3
4
5
6

下面的例子打印文件中的行的總數(shù):

[jerry]$ sed -n '$='  books.txt

當(dāng)執(zhí)行上面的代碼,會得到如下結(jié)果:

6

& 命令

Sed 支持特殊字符和存儲匹配的模式,每當(dāng)一個模式匹配成功。它經(jīng)常被用于替代命令??纯慈绾文軌蚶眠@種高效的特點。

在book.txt文件中的每一行編號。添加詞語數(shù)量在每一行的開頭。下面的例子說明了這一點:

[jerry]$ sed 's/[[:digit:]]/Book number &/' books.txt

當(dāng)執(zhí)行上面的代碼,會得到如下結(jié)果:

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

這個例子是很簡單的。首先尋找一個數(shù)字,這是行號的第一次出現(xiàn)(這就是為什么使用[[:數(shù)字:]])和桑達(dá)自動存儲在特殊字符和匹配模式。在第二步驟中,我們將插入每個匹配的模式,也就是說,每行之前之前詞語的數(shù)量。

再舉一個例子。在book.txt文件,最后一個數(shù)字是書的頁數(shù)。在這之前加上“Pages=”。要做到這一點,找到數(shù)字的最后一次出現(xiàn),并用“Pages=&”代替。這里,&存儲匹配模式,即,頁面的數(shù)量

[jerry]$ sed 's/[[:digit:]]*$/Pages = &/' books.txt 

當(dāng)執(zhí)行上面的代碼,會得到如下結(jié)果:

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

從目前來看,只記得[[:數(shù)字:]]* $找到數(shù)字的最后出現(xiàn)。在該章中的“正則表達(dá)式中,我們將探討更多的正則表達(dá)式。


上一篇:Sed循環(huán)下一篇:Sed分支