我們對任何文件進行基本操作,顯示其內(nèi)容。為了達到這個目的,我們可以用打印的模式緩沖區(qū)的打印命令。本教程將介紹更多的模式緩沖區(qū),以及如何打印使用相關模式緩沖區(qū)不同運算符的文件的內(nèi)容。
考慮一下我們有一個文本文件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
我們可以用Sed 'p'命令來打印指定文件的內(nèi)容。下面是一個簡單的命令來打印文件 books.txt 的內(nèi)容。
[jerry]$ sed 'p' books.txt
當執(zhí)行上面的代碼,就會產(chǎn)生下面的結果。
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打印模式緩沖區(qū)的內(nèi)容。此外,我們已明確地接入命令部分 print 命令。因此,每行打印了兩次。Sed有一個-n選項來抑制模式緩沖區(qū)的默認打印。讓我們試試下面的命令:
[jerry]$ sed -n 'p' books.txt
當執(zhí)行上面的代碼,就會產(chǎn)生下面的結果。
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 命令前一個地址范圍。
[jerry]$ sed -n '3p' books.txt
當執(zhí)行上面的代碼,就會產(chǎn)生下面的結果。
3) The Alchemist, Paulo Coelho, 197
下面的代碼打印2?5。這里我們使用了逗號(,)運算符指定的地址范圍內(nèi)的所有行:
[jerry]$ sed -n '2,5 p' books.txt
當執(zhí)行上面的代碼,就會產(chǎn)生下面的結果。
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
我們可以使用美元符號$運算符來打印該文件的最后一行,如下所示:
[jerry]$ sed -n '$ p' books.txt
當執(zhí)行上面的代碼,就會產(chǎn)生下面的結果。
6) A Game of Thrones, George R. R. Martin, 864
但是,我們也可以使用美元符號($)來指定一個地址范圍。下面的例子打印通過第3行到最后一行。
[jerry]$ sed -n '3,$ p' books.txt
當執(zhí)行上面的代碼,就會產(chǎn)生下面的結果。
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將打印的行數(shù)M,以下示例開始打印從第2行開始到下一個4行:
[jerry]$ sed -n '2,+4 p' books.txt
當執(zhí)行上面的代碼,就會產(chǎn)生下面的結果。
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
或者,我們也可以使用波浪符號(?)運算符指定的地址范圍。它采用M?n形式。這表明 sed 應該開始行數(shù)M和處理每n(次)行。例如,50?5行號50,55,60,65,等等。讓我們從打印的文件只有奇數(shù)行。
[jerry]$ sed -n '1~2 p' books.txt
當執(zhí)行上面的代碼,就會產(chǎn)生下面的結果。
1) A Storm of Swords, George R. R. Martin, 1216 3) The Alchemist, Paulo Coelho, 197 5) The Pilgrimage, Paulo Coelho, 288
下面的代碼僅打印偶數(shù)行的文件。
[jerry]$ sed -n '2~2 p' books.txt
當執(zhí)行上面的代碼,就會產(chǎn)生下面的結果。
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