sed教程(七)之特殊字符

Sed提供了被看成命令的兩個特殊字符。本章說明了這兩個特殊字符的使用。嘗試使用這些命令,考慮有一個文本文件books.txt待處理,它有如下內容:git

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

= 命令

=命令將行號打印到標準輸出流。下面給出的是=命令的語法:正則表達式

[address1[,address2]]=

這裏address1 和 address2分別爲起始和結束地址,其能夠是行號或模式串。這兩個地址是可選參數,若是不提供它們做爲前綴=命令,以下圖所示,將打印的全部行的行號:spa

[jerry]$ sed '=' books.txt 

當執行上面的代碼,會獲得以下結果:orm

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行的行號和剩餘不帶行號:ip

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

當執行上面的代碼,會獲得以下結果:it

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」的行號。class

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

當執行上面的代碼,會獲得以下結果:sed

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

當執行上面的代碼,會獲得以下結果:im

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

當執行上面的代碼,會獲得以下結果:

1
2
3
4
5
6

下面的例子打印文件中的行的總數:

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

當執行上面的代碼,會獲得以下結果:

6

& 命令

Sed 支持特殊字符和存儲匹配的模式,每當一個模式匹配成功。它常常被用於替代命令。看看如何可以利用這種高效的特色。

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

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

當執行上面的代碼,會獲得以下結果:

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 

這個例子是很簡單的。首先尋找一個數字,這是行號的第一次出現(這就是爲何使用[[:數字:]])和桑達自動存儲在特殊字符和匹配模式。在第二步驟中,咱們將插入每一個匹配的模式,也就是說,每行以前以前詞語的數量。

再舉一個例子。在book.txt文件,最後一個數字是書的頁數。在這以前加上「Pages=」。要作到這一點,找到數字的最後一次出現,並用「Pages=&」代替。這裏,&存儲匹配模式,即,頁面的數量

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

當執行上面的代碼,會獲得以下結果:

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 

從目前來看,只記得[[:數字:]]* $找到數字的最後出現。在該章中的「正則表達式中,咱們將探討更多的正則表達式。

相關文章
相關標籤/搜索