我們對任何檔進行基本操作,顯示其內容。為了達到這個目的,我們可以用列印的模式緩衝區的列印命令。本教學將介紹更多的模式緩衝區,以及如何列印使用相關模式緩衝區不同運算符的檔的內容。
考慮一下我們有一個文本檔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
Sed p 命令
我們可以用Sed 'p'命令來列印指定檔的內容。下麵是一個簡單的命令來列印檔 books.txt 的內容。
[jerry]$ sed 'p' books.txt
當執行上面的代碼,就會產生下麵的結果。
1) A Storm of Swords, George R. R. Martin, 1216 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 6) A Game of Thrones, George R. R. Martin, 864
讓我們找出為什麼每個行被列印兩次。實際上,在默認情況下,sed列印模式緩衝區的內容。此外,我們已明確地接入命令部分 print 命令。因此,每行列印了兩次。Sed有一個-n選項來抑制模式緩衝區的默認列印。讓我們試試下麵的命令:
[jerry]$ sed -n 'p' 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
Sed 地址範圍
默認情況下,sed在所有行上操作。但是,可以強制sed只在某些行上使用。例如,在下面的例子中,sed只運行在第三行。在這個例子中,我們指定 sed 命令前一個地址範圍。
[jerry]$ sed -n '3p' books.txt
當執行上面的代碼,就會產生下麵的結果。
3) The Alchemist, Paulo Coelho, 197
Sed comma 命令
下麵的代碼列印2〜5。這裏我們使用了逗號(,)運算符指定的地址範圍內的所有行:
[jerry]$ sed -n '2,5 p' books.txt
當執行上面的代碼,就會產生下麵的結果。
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
Sed $ 運算符
我們可以使用美元符號$運算符來列印該檔的最後一行,如下所示:
[jerry]$ sed -n '$ p' books.txt
當執行上面的代碼,就會產生下麵的結果。
6) A Game of Thrones, George R. R. Martin, 864
但是,我們也可以使用美元符號($)來指定一個地址範圍。下麵的例子列印通過第3行到最後一行。
[jerry]$ sed -n '3,$ p' books.txt
當執行上面的代碼,就會產生下麵的結果。
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
Sed 加操作
sed的加號(+)運算符可以用來與逗號(,)運算符。例如M,+ n將列印的行數M,以下示例開始列印從第2行開始到下一個4行:
[jerry]$ sed -n '2,+4 p' books.txt
當執行上面的代碼,就會產生下麵的結果。
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
Sed 波浪線運算符
或者,我們也可以使用波浪符號(〜)運算符指定的地址範圍。它採用M〜n形式。這表明 sed 應該開始行數M和處理每n(次)行。例如,50〜5行號50,55,60,65,等等。讓我們從列印的檔只有奇數行。
[jerry]$ sed -n '1~2 p' books.txt
當執行上面的代碼,就會產生下麵的結果。
1) A Storm of Swords, George R. R. Martin, 1216 3) The Alchemist, Paulo Coelho, 197 5) The Pilgrimage, Paulo Coelho, 288
下麵的代碼僅列印偶數行的檔。
[jerry]$ sed -n '2~2 p' books.txt
當執行上面的代碼,就會產生下麵的結果。
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