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 

从目前来看,只记得[[:数字:]]* $找到数字的最后出现。在该章中的“正则表达式中,我们将探讨更多的正则表达式。

联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2022

备案号:京ICP备15003423号-3