Sed特殊字元


Sed提供了被當作命令兩個特殊字元。本章說明了這兩個特殊字元的使用。嘗試使用這些命令,考慮有一個文字檔案books.txt待處理,它有以下內容:

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分別為起始和結束地址,其可以是行號或模式串。這兩個地址是可選引數,如果不提供它們作為字首=命令,如下圖所示,將列印的所有行的行號:

[jerry]$ sed '=' books.txt 

當執行上面的程式碼,會得到如下結果:

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 

當執行上面的程式碼,會得到如下結果:

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 

當執行上面的程式碼,會得到如下結果:

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

當執行上面的程式碼,會得到如下結果:

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 

從目前來看,只記得[[:數位:]]* $找到數位的最後出現。在該章中的“正規表示式中,我們將探討更多的正規表示式。