Sed - 模式缓冲区

Sed - 模式缓冲区 首页 / Sed入门教程 / Sed - 模式缓冲区

无涯教程对任何文件执行的基本操作之一是显示其内容。为此,可以使用 print 命令来打印 Pattern Buffer模式缓冲区 的内容。

首先创建一个文件,其中包含行号,书名,作者和页数。在本教程中,将使用此文件,文本文件将如下所示:

[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

执行以上代码后,将产生以下输出。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-pattern-buffer.html

来源:LearnFk无涯教程网

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打印Pattern Buffer模式缓冲区的内容。此外,在命令部分中明确包含了一条打印命令。因此,每行打印两次。但是不用担心。 SED具有 -n 选项,以禁止缓冲区的默认打印。以下命令说明了这一点。

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

执行以上代码后,将产生以下输出。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-pattern-buffer.html

来源:LearnFk无涯教程网

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命令之前指定了Address Range(地址范围)

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

执行以上代码后,将产生以下输出。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-pattern-buffer.html

来源:LearnFk无涯教程网

3) The Alchemist, Paulo Coelho, 197 

此外,还可以指示SED仅打印某些行。如下面的代码打印2到5的所有行。在这里,使用 逗号(,) 运算符指定地址范围。

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

执行以上代码后,将产生以下输出。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-pattern-buffer.html

来源:LearnFk无涯教程网

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

还有一个特殊字符Dollar($)代表文件的最后一行。因此,让无涯教程打印文件的最后一行。

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

执行以上代码后,将产生以下输出。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-pattern-buffer.html

来源:LearnFk无涯教程网

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

但是,也可以使用Dollar($) 字符指定地址范围。下面的示例通过第3行打印到最后一行。

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

执行以上代码后,将产生以下输出。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-pattern-buffer.html

来源:LearnFk无涯教程网

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支持两个以上的运算符,可用于指定地址范围。首先是plus(+)运算符,它可以与逗号(,)运算符一起使用。如 M,+ n 将从行号 M 开始打印下一个 n 行。听起来令人困惑?让用一个简单的示例。以下示例从第2行开始打印接下来的4行。

[jerry]$sed -n '2,+4 p' books.txt 

执行以上代码后,将产生以下输出。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-pattern-buffer.html

来源:LearnFk无涯教程网

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(th)行处理一次。如, 50〜5 匹配行号50、55、60、65,依此类推。仅从文件中打印奇数行。

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

执行以上代码后,将产生以下输出。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-pattern-buffer.html

来源:LearnFk无涯教程网

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 

执行以上代码后,将产生以下输出。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-pattern-buffer.html

来源:LearnFk无涯教程网

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 

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

AI技术内参 -〔洪亮劼〕

赵成的运维体系管理课 -〔赵成〕

Linux性能优化实战 -〔倪朋飞〕

Vue开发实战 -〔唐金州〕

Swift核心技术与实战 -〔张杰〕

流程型组织15讲 -〔蒋伟良〕

中间件核心技术与实战 -〔丁威〕

现代React Web开发实战 -〔宋一玮〕

JavaScript进阶实战课 -〔石川〕

好记忆不如烂笔头。留下您的足迹吧 :)