流编辑器 - 基本命令

本章介绍了几个有用的SED命令.

删除命令

SED提供了各种操作文本的命令.我们先来探讨一下 delete 命令.以下是执行删除命令的方法:

[address1[,address2]]d

address1 address2 分别是起始地址和结束地址,可以是行号或模式字符串.这两个地址都是可选参数.

顾名思义,delete命令用于执行删除操作,由于SED在线运行,我们可以说这个命令用于删除行.请注意,delete命令仅从模式缓冲区中删除行;该行不会发送到输出流,原始文件保持不变.以下示例说明了这一点.

[jerry]$ sed 'd' books.txt

但输出在哪里?如果没有提供行地址,则SED默认在每一行上运行.因此,它从模式缓冲区中删除所有行.这就是为什么命令不会在标准输出上打印任何内容.

让我们指示SED仅在某些行上运行.以下示例仅删除第4行.

[jerry]$ sed '4d' 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 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

此外,SED还接受使用逗号(,)的地址范围.我们可以指示SED将N1移除到N2线.例如,以下示例删除从2到4的所有行.

[jerry]$ sed '2, 4 d' books.txt

在执行上述代码时,您会得到以下结果:

1) A Storm of Swords, George R. R. Martin, 1216 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

SED的地址范围不仅限于数字.我们还可以将模式指定为地址.以下示例删除了作者Paulo Coelho的所有书籍.

[jerry]$ sed '/Paulo Coelho/d' books.txt

在执行上述代码时,您会得到以下结果:

1) A Storm of Swords, George R. R. Martin, 1216 
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

我们还可以使用文本模式指定地址范围.以下示例删除模式之间的所有行风暴团契.

[jerry]$ sed '/Storm/,/Fellowship/d' books.txt  
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

除此之外,我们还可以在SED中使用美元($),加(+)和波浪号(〜)运算符.

写命令

我们对任何文件执行的一项重要操作是备份,即我们制作该文件的另一个副本. SED提供 write 命令将模式缓冲区的内容存储在文件中.下面给出的是 write 命令的语法,类似于 delete 命令.

[address1[,address2]]w file

此处,地址1 地址2 是分别是起始地址和结束地址,可以是行号或模式字符串.这两个地址都是可选参数.

在上面的语法中, w 指的是写命令,文件是文件名在其中存储内容.小心文件参数.当提供文件名时,如果文件不存在,SED会动态创建文件,如果文件已经存在则覆盖它.

让我们制作文件的精确副本使用SED.请注意, w 文件之间必须只有一个空格.

[jerry]$ sed -n 'w books.bak' books.txt

我们创建了另一个名为 books.bak的文件.现在验证两个文件都有相同的内容.

[jerry]$ diff books.txt books.bak  
[jerry]$ echo $?

在执行上述代码时,您会得到以下结果:

0

您可以假设 cp 命令执行完全相同的操作.是! cp 命令执行相同的操作,但SED是一个成熟的实用程序.它允许创建仅包含源文件中某些行的文件.让我们只将偶数行存储到另一个文件.

[jerry]$ sed -n '2~2 w junk.txt' books.txt  
[jerry]$ cat junk.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

您还可以使用逗号(,),美元($)和加号(+)运算符与write命令.

除此之外,SED还支持使用write命令进行模式匹配.假设您要将单个作者的所有书籍存储到单独的文件中.一种无聊且冗长的方式是手动操作,更聪明的方法是使用SED.

[jerry]$ sed -n -e '/Martin/ w Martin.txt' -e '/Paulo/ w Paulo.txt' -e '/Tolkien/ w 
Tolkien.txt' books.txt

在上面的例子中,我们将每一行与模式匹配并将匹配的行存储在特定文件中.这很简单.要指定多个命令,我们使用了SED命令的 -e 开关.现在让我们看看每个文件包含的内容:

[jerry]$ cat Martin.txt

在执行上述代码时,您会得到以下结果:

1) A Storm of Swords, George R. R. Martin, 1216 
6) A Game of Thrones, George R. R. Martin, 864

让我们显示文件内容.

[jerry]$ cat Paulo.txt

执行上面的代码时,会得到以下结果:

3) The Alchemist, Paulo Coelho, 197 
5) The Pilgrimage, Paulo Coelho, 288

让我们显示文件内容.

[jerry]$ cat Tolkien.txt

在执行上述代码时,您会得到以下结果:

2) The Two Towers, J. R. R. Tolkien, 352 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432

非常好!我们得到了预期的结果. SED确实是一个了不起的实用程序.

追加命令

任何文本编辑器最有用的操作之一是提供追加功能. SED通过append命令支持此操作.下面给出了append的语法:

[address]a\ 
Append text

让我们在第4行后添加一本新书.下面的例子说明了如何做到这一点

[jerry]$ sed '4 a 7) Adultry, Paulo Coelho, 234' 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 
7) Adultry, Paulo Coelho, 234 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

在命令部分, 4 表示行号, a 是append命令,剩下的部分是要追加的文本.

让我们在文件的末尾插入一个文本行.为此,请使用 $ 作为地址.以下示例说明了这一点:

[jerry]$ sed '$ a 7) Adultry, Paulo Coelho, 234' 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 
7) Adultry, Paulo Coelho, 234

除了行号,我们还可以使用文本模式指定地址.例如,以下示例在匹配字符串 The Alchemist 后附加文本.

[jerry]$ sed '/The Alchemist/ a 7) Adultry, Paulo Coelho, 234' 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 
7) Adultry, Paulo Coelho, 234 
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 '/The/ a 7) Adultry, Paulo Coelho, 234' books.txt

在执行上述代码时,您会得到以下结果:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
7) Adultry, Paulo Coelho, 234 
3) The Alchemist, Paulo Coelho, 197 
7) Adultry, Paulo Coelho, 234 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
7) Adultry, Paulo Coelho, 234 
5) The Pilgrimage, Paulo Coelho, 288 
7) Adultry, Paulo Coelho, 234 
6) A Game of Thrones, George R. R. Martin, 864

更改命令

SED提供更改替换命令,该命令由c表示.此命令有助于用新文本替换现有行.提供行范围时,所有行都由一个文本行替换为一个组.下面给出了更改命令的语法:

[address1[,address2]]c\ 
Replace text

让我们用其他文本替换第三行.

[jerry]$ sed '3 c 3) Adultry, Paulo Coelho, 324' books.txt

在执行上述代码时,您会得到以下结果:

 
 1)风暴之剑,George RR Martin,1216 
 2)双塔,JRR Tolkien,352 
 3)Adultry,保罗科埃略,324 
 4)环形团契,JRR托尔金,432 
 5)朝圣,保罗科埃略,288 
 6)权力的游戏,乔治RR马丁,864

SED也接受模式作为地址.在以下示例中,当模式匹配成功时,将替换一行.

 
 [jerry] $ sed'/The Alchemist/c 3)Adultry ,Paulo Coelho,324'books.txt

在执行上述代码时,您会得到以下结果:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) Adultry, Paulo Coelho, 324 
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还允许用一条线替换多条线.以下示例从第四行到第六行删除行,并用新文本替换它们.

[jerry] $ sed'4,6 c 4)Adultry ,Paulo Coelho,324'books.txt

在执行上述代码时,您会得到以下结果:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
3) Adultry, Paulo Coelho, 324 
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

插入命令

insert命令的工作原理大致相同如追加一样.唯一的区别是它在特定位置之前插入一条线.下面给出了insert命令的语法:

[address]i\ 
Insert text

让我们通过一些例子来理解insert命令.以下命令在第四行之前插入一个新条目.

[jerry]$ sed '4 i 7) Adultry, Paulo Coelho, 324' 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 
7) Adultry, Paulo Coelho, 324 
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

在上面的示例中, 4 是位置编号, i 表示插入命令,以及剩余部分是要插入的文本.

要在文件开头插入文本,请提供行地址 1 .以下命令说明了这一点:

[jerry]$ sed '1 i 7) Adultry, Paulo Coelho, 324' books.txt

在执行上述代码时,您会得到以下结果:

7) Adultry, Paulo Coelho, 324

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 '$ i 7) Adultry, Paulo Coelho, 324

在执行上述代码时,您会得到以下结果:

8) Eleven Minutes, Paulo Coelho, 304' 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 
7) Adultry, Paulo Coelho, 324 
8) Eleven Minutes, Paulo Coelho, 304 
6) A Game of Thrones, George R. R. Martin, 864

请注意,要插入的条目是在单独的行中输入并由反斜杠(\)字符分隔.

翻译命令

SED提供翻译字符的命令,并表示如 y .它按位置转换字符.下面给出的是translate命令的语法:

[address1[,address2]]y/list-1/list-2/

请注意,翻译是基于列表1 中的字符到列表2

以下示例将阿拉伯数字转换为罗马数字.

[jerry]$ echo "1 5 15 20" | sed 'y/151520/IVXVXX/'

在执行上述代码时,您会得到以下结果:

I V IV XX

l命令

你能区分分隔的单词吗?只有通过查看它们,空格和单词由制表符分隔?当然不是.但是SED可以为你做到这一点. SED使用 l 命令在文本中显示隐藏的字符.例如,带有 \t 的制表符和带有 $ 字符的行尾.下面给出了 l 命令的语法.

[address1[,address2]]l 
[address1[,address2]]l [len]

让我们创建一个带有制表符的文件进行演示.为简单起见,我们将使用相同的文件,只需用制表符替换空格即可.等待!但是如何做到这一点 - 在文本编辑器中打开文件并用标签替换每个空格?当然不是!我们可以使用SED命令.

[jerry]$ sed 's/ /\t/g' books.txt > junk.txt

现在让我们使用 l 命令显示隐藏的字符:

[jerry]$ sed -n 'l' junk.txt

执行上述代码时,你得到以下结果:

1)\tA\tStorm\tof\tSwords,George\tR.\tR.\tMartin,1216$ 
2)\tThe\tTwo\tTowers,J.\tR.\tR.\tTolkien,352$ 
3)\tThe\tAlchemist,Paulo\tCoelho,197$ 
4)\tThe\tFellowship\tof\tthe\tRing,J.\tR.\tR.\tTolkien,432$ 
5)\tThe\tPilgrimage,Paulo\tCoelho,288$ 
6)\tA\tGame\tof\tThrones,George\tR.\tR.\tMartin\t,864$

与其他SED命令一样,它也接受行麻木作为地址的人和模式.你可以自己尝试一下.

让我们仔细看看SED的另一个有趣特征.我们可以指示SED在一定数量的字符后执行换行.以下示例包含25个字符后的行.

[jerry]$ sed -n 'l 25' books.txt

在执行上述代码时,您会得到以下结果:

1) A Storm of Swords,Geo\ 
rge R. R. Martin,1216$ 
2) The Two Towers,J. R. \ 
R. Tolkien,352$ 
3) The Alchemist,Paulo C\ 
oelho,197$ 
4) The Fellowship of the\ 
 Ring,J. R. R. Tolkien,4\ 
32$ 
5) The Pilgrimage,Paulo \ 
Coelho,288$ 
6) A Game of Thrones,Geo\ 
rge R. R. Martin ,864$

请注意,在上面的示例中,在l命令之后提供了wrap限制.在这种情况下,它是25个字符.此选项是特定于GNU的,可能不适用于SED的其他变体.

换行限制为0表示除非有新的换行符,否则永远不会断行.以下简单的命令说明了这一点.

[jerry]$ sed -n 'l 0' 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退出当前执行流程.它由 q 命令表示.下面给出了退出命令的语法:

[address]q 
[address]q [value]

请注意,quit命令不接受地址范围,它只支持单个地址.默认情况下,SED遵循读取,执行和重复工作流程;但是当遇到quit命令时,它只会停止当前的执行.

让我们打印文件中的前3行.

[jerry]$ sed '3 q' 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

除了行号,我们还可以使用文本模式.当模式匹配成功时,以下命令退出.

[jerry]$ sed '/The Alchemist/ q' 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

除此之外,SED还可以接受,它可以用作退出状态.以下命令将其退出状态显示为100.

[jerry]$ sed '/The Alchemist/ q 100' 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

现在让我们验证退出状态.

[jerry]$ echo $?

在执行上述代码时,您会得到以下结果:

100

读取命令

我们可以指示SED读取文件的内容并在特定时显示它们条件匹配.该命令由字母 r 表示.下面给出了read命令的语法.

[address]r file

请注意, r 命令和文件名之间必须只有一个空格.

让我们通过一个简单的例子来理解它.创建一个名为 junk.txt 的示例文件.

[jerry]$ echo "This is junk text." > junk.txt

以下命令指示SED读取 junk.txt 的内容并在第三行后插入.

[jerry]$ sed '3 r junk.txt' books.txt

在执行上述代码时,您会得到以下结果:

 
 1)剑风暴,George RR Martin,1216 
 2)The Two Towers,JRR Tolkien,352 
 3)Alchemist,Paulo Coelho,197 
这是垃圾文本. 
 4)环形团契,JRR托尔金,432 
 5)朝圣,保罗科埃略,288 
 6)权力的游戏,乔治RR马丁,864

在上面的例子中,3表示行地址, r 是命令名称, junk.txt 是文件名,其内容将被显示.此外,GNU SED还接受一系列地址.例如,以下命令在第三行,第四行和第五行之后插入 junk.txt 的内容.

 
 [jerry] $ sed'3,5 runk.txt'books.txt

在执行上述代码时,您会得到以下结果:

 
 1)风暴之剑,George RR Martin,1216 
 2)双塔,JRR Tolkien,352 
 3)炼金术士Paul Coelho,197 
这是垃圾文本. 
 4)The Ring of Fellowship of the Ring,J.R. R. Tolkien,432 
这是垃圾文本. 
 5)The Pilgrimage,Paulo Coelho,288 
这是垃圾文本. 
 6)权力的游戏,George R. R. Martin,864

与其他SED命令一样,read命令也接受模式作为地址.例如,当模式匹配成功时,以下命令插入 junk.txt 的内容.

 
 [jerry] $ sed'/Paulo/r junk.txt'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 
This is junk text. 
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的外部命令使用执行命令.它由 e 表示.下面给出了execute命令的语法.

[address1[,address2]]e [command]

让我们用一个简单的例子说明execute命令.以下SED命令在第三行之前执行UNIX date 命令.

[jerry]$ sed '3 e date' books.txt

在执行上述代码时,您会得到以下结果:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
Sun Sep  7 18:04:49 IST 2014 
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

与其他命令一样,它也接受模式作为地址.例如,以下示例在模式匹配成功时执行 date 命令.请注意,在每个模式匹配之后,首先执行命令,然后显示模式缓冲区的内容.

[jerry]$ sed '/Paulo/ e date' books.txt

在执行上述代码时,您会得到以下结果:

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
Sun Sep  7 18:04:49 IST 2014 
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

如果你观察 e的语法命令,你会注意到命令是可选的.如果在 e之后没有提供命令,它会将模式缓冲区的内容视为外部命令.为了说明这一点,让我们用一些简单的命令创建一个commands.txt文件.

[jerry]$ echo -e "date\ncal\nuname" > commands.txt 
[jerry]$ cat commands.txt

执行上面的代码时,得到以下结果:

date 
cal 
uname

该文件中的命令不言自明.在 e之后没有命令时, SED逐个执行所有这些命令.以下简单示例说明了这一点.

[jerry]$ sed 'e' commands.txt

在执行上述代码时,您会得到以下结果:

Sun Sep  7 18:14:20 IST 2014 
   September 2014      
Su Mo Tu We Th Fr Sa   
    1  2  3  4  5  6   
 7  8  9 10 11 12 13   
14 15 16 17 18 19 20   
21 22 23 24 25 26 27   
28 29 30               
                       
Linux

与其他SED命令一样,执行命令也接受所有有效的地址范围.

其他命令

默认情况下,SED在单行上运行,但它可以在多行上运行好.多行命令用大写字母表示.例如,与 n 命令不同, N 命令不会清除并打印模式空间.相反,它在当前模式空间的末尾添加换行符(\ n),并将输入文件中的下一行追加到当前模式空间,并通过执行其余的SED命令继续执行SED的标准流程.下面给出了 N 命令的语法.

[address1[,address2]]N

让我们打印以逗号分隔的书名列表及其各自的作者.以下示例说明了这一点.

[jerry]$ sed 'N; s/\n/, /g' books.txt

在执行上述代码时,您会得到以下结果:

A Storm of Swords, George R. R. Martin 
The Two Towers, J. R. R. Tolkien 
The Alchemist, Paulo Coelho 
The Fellowship of the Ring, J. R. R. Tolkien 
The Pilgrimage, Paulo Coelho 
A Game of Thrones, George R. R. Martin

让我们了解上述示例的工作原理. N 命令将第一行(即风暴之风)读入模式缓冲区并附加\ n后跟下一行.模式空间现在包含剑风暴 \ n George R. R. Martin.在下一步中,我们用逗号替换换行符.

p 命令一样,我们有一个 P 命令来打印由 N 命令创建的多行模式空间的第一部分(直到嵌入换行符).下面给出的是 P 命令的语法,类似于 p 命令.

[address1[,address2]]P

在前面的例子中,我们看到 N 命令创建了一个换行符书名和作者名单.让我们只打印它的第一部分,即只打印书的标题.以下命令说明了这一点.

[jerry] $ sed -n'N; P'books.txt

在执行上述代码时,您会得到以下结果:

A Storm of Swords 
The Two Towers 
The Alchemist 
The Fellowship of the Ring 
The Pilgrimage 
A Game of Thrones

请注意,在没有 N 的情况下,它的行为与 p 命令相同. The following simple command illustrates this scenario.

[jerry]$ sed -n ’P’ books.txt

执行上面的代码时,您会得到以下结果:

 A Storm of Swords  
 George R. R. Martin  
 The Two Towers  
 J. R. R. Tolkien  
 The Alchemist  
 Paulo Coelho  
 The Fellowship of the Ring  
 J. R. R. Tolkien  
 The Pilgrimage  
 Paulo Coelho  
 A Game of Thrones  
 George R. R. Martin

除此之外,SED还提供了一个检查版本的v命令。 如果提供的版本大于安装的SED版本,则命令执行失败。 请注意,此选项是GNU特定的,可能无法与其他SED变体一起使用。 下面给出的是v命令的语法。

[address1[,address2]]v [version]

首先,找出当前版本的SED

 [jerry]$ sed --version

执行上面的代码时,您会得到以下结果:

 sed (GNU sed) 4.2.2

在以下示例中,SED版本大于版本4.2.2,因此SED命令中止其执行.

 
 [jerry]$ sed ’v 4.2.3’ books.txt

执行上面的代码时,您会得到以下结果:

 
 sed: -e expression #1, char 7: expected newer version of sed

但是,如果提供的版本小于或等于版本4.2.2,则命令按预期工作.

 
 [jerry]$ sed ’v 4.2.2’ books.txt

执行上面的代码时,您会得到以下结果:

 
 A Storm of Swords  
 George R. R. Martin  
 The Two Towers  
 J. R. R. Tolkien  
 The Alchemist  
 Paulo Coelho  
 The Fellowship of the Ring  
 J. R. R. Tolkien  
 The Pilgrimage  
 Paulo Coelho  
 A Game of Thrones George R. R. Martin