流编辑器 - 模式缓冲区

我们对任何文件执行的基本操作之一是显示其内容.为此,我们可以使用 print 命令打印模式缓冲区的内容.因此,让我们更多地了解模式缓冲区

首先创建一个包含行号,书名,作者和页数的文件.在本教程中,我们将使用此文件.您可以根据自己的方便使用任何文本文件.我们的文本文件如下所示:

[jerry]$ vi 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

现在,让我们打印文件内容.

[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的工作流程吗?默认情况下,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仅在第3行上运行.在这个例子中,我们在SED命令之前指定了一个地址范围.

[jerry]$ sed -n '3p' books.txt

执行上述代码时,会产生以下结果.

3) The Alchemist, Paulo Coelho, 197

此外,我们还可以指示SED仅打印某些行.例如,以下代码打印从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

还有一个特殊字符美元($)它代表文件的最后一行.所以,让我们打印文件的最后一行.

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

执行上述代码时,会产生以下结果.

6) A Game of Thrones, George R. R. Martin, 864

但是我们也可以使用Dollar($)字符来指定地址范围.下面的例子通过第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支持另外两个可用于指定地址范围的运算符.首先是加号(+)运算符,它可以与逗号(,)运算符一起使用.例如, M,+ n 将打印从行号 M 开始的下一行 n 行.听起来很混乱?让我们用一个简单的例子来检查它.以下示例打印从第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

我们也可以使用以下方式指定地址范围代字号(〜)运算符.它使用 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