Sed - 基本命令

Sed - 基本命令 首页 / Sed入门教程 / Sed - 基本命令

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

Delete命令

SED提供各种命令来操作文本。让无涯教程首先探讨 delete 命令。这是执行删除命令的方式:

[address1[,address2]]d 

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

注意(,) delete 命令仅从模式缓冲区中删除行,而不会覆盖文件内容。以下示例说明了这一点。

[jerry]$sed 'd' books.txt 

但是为什么不有输出内容?因为默认情况下SED在每行上运行,但没有输出命令。

下面的示例仅删除第4行。

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

此外,SED还使用 逗号(,) 接受 地址范围 。例如,以下示例从2到4删除所有行。

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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 

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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 

无涯教程还可以使用文本模式指定地址范围。以下示例删除了"Storm"和"Fellowship"之间的所有行。

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

Write命令

对任何文件执行的重要操作之一就是备份,制作了该文件的另一个副本。 SED提供 write 命令以将模式缓冲区的内容存储在文件中。下面给出的是 write 命令的语法,它与 delete 命令相似。

[address1[,address2]]w file 

在这里(,) address1 和 address2 分别是开始地址和结束参数,这两个参数都是可选的。

在以上语法中(,) w 表示写入命令,而 file 是用于存储内容的文件名。请小心 file 参数,提供文件名后,如果文件名不存在,SED会即时创建一个文件,如果文件名已存在,则SED会覆盖它。

让无涯教程使用SED复制文件。请注意(,) w 和文件之间必须恰好有一个空格。

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

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

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

0

 cp 命令执行的操作完全相同。它允许创建一个仅包含源文件中某些行的文件。仅将偶数行存储到另一个文件中。

[jerry]$sed -n '2~2 w junk.txt' books.txt  
[jerry]$cat junk.txt 

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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 

您还可以在write命令中使用 (,) 美元($) 加号(+)运算符。

除此之外,SED还支持使用write命令进行模式匹配。假设您要将各个作者的所有书籍都存储到一个单独的文件中。

[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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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

让无涯教程显示文件内容。

[jerry]$cat Paulo.txt

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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

显示文件内容。

[jerry]$cat Tolkien.txt

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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

Append命令

任何文本编辑器最有用的操作之一就是提供附加函数。 SED通过其append命令支持此操作。下面给出了append的语法:

[address]a\
Append text 

在第4行之后附加一个新书条目。下面的示例显示了如何做

[jerry]$sed '4 a 7) Adultry, Paulo Coelho, 234' books.txt 

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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 
7) Adultry, Paulo Coelho, 234 
5) The Pilgrimage, Paulo Coelho, 288 
6) A Game of Thrones, George R. R. Martin, 864

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

[jerry]$sed '$a 7) Adultry, Paulo Coelho, 234' books.txt

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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 
7) Adultry, Paulo Coelho, 234 

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

[jerry]$sed '/The Alchemist/a 7) Adultry, Paulo Coelho, 234' books.txt  

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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 
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 

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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 

Change 命令

SED提供由c表示的 changereplace 命令。此命令有助于用新文本替换现有行。下面给出的是change命令的语法:

[address1[,address2]]c\
Replace text

让无涯教程用其他一些文本代替第三行。

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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 '/The Alchemist/c 3) Adultry, Paulo Coelho, 324' books.txt

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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  

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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) Adultry, Paulo Coelho, 324

Insert 命令

insert命令的工作方式与append相同。唯一的区别是,它在特定位置之前插入了一行。下面是插入命令的语法:

[address]i\
Insert text 

通过一些示例来了解insert命令。以下命令在第四行之前插入一个新条目。

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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 
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

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

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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

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

Translate 命令

SED提供Translate字符的命令,它表示为 y ,它按位置转换字符,下面给出的是translate命令的语法:

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

请注意(,) Translate是基于字符从list-1 到list-2 中相同位置的字符的位置,并且两个列表都必须是显式字符列表,不支持正则表达式和字符类。此外,list-1 和list-2 的大小必须相同。

下面的示例将阿拉伯数字转换为罗马数字。

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

I V IV XX 

l 命令

 SED使用 l 命令在文本中显示隐藏的字符。例如,带有\t 的制表符和带有 $字符的行尾。下面给出的是 l 命令的语法。

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

创建一个带有制表符的文件进行演示。为简单起见,将使用相同的文件,只是用制表符替换空格。

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

现在,让无涯教程使用 l 命令显示隐藏的字符:

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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命令之后提供了长度限制。 在这种情况下,它是25个字符。 此选项特定于GNU中使用。

换行限制为0意味着除非有换行符,否则切勿中断换行。以下简单命令说明了这一点。

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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$

Quit 命令

退出命令指示SED退出当前执行流程。它由 q 命令表示。以下是quit命令的语法:

[address]q 
[address]q [value]

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

让无涯教程打印文件的前3行。

[jerry]$sed '3 q' books.txt

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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

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

[jerry]$sed '/The Alchemist/q' books.txt 

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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

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

[jerry]$sed '/The Alchemist/q 100' books.txt

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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

现在让无涯教程验证退出状态。

[jerry]$echo $? 

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

100

Read 命令

可以指示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 

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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 
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

在上面的示例中,3表示行地址(,) r 是命令名称,而 junk.txt 是要显示其内容的文件名。例如,以下命令将 junk.txt 的内容插入第三,第四和第五行之后。

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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 
This is junk text. 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
This is junk text. 
5) The Pilgrimage, Paulo Coelho, 288 
This is junk text. 
6) A Game of Thrones, George R. R. Martin, 864

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

[jerry]$sed '/Paulo/r junk.txt' books.txt  

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.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 
This is junk text. 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
5) The Pilgrimage, Paulo Coelho, 288 
This is junk text. 
6) A Game of Thrones, George R. R. Martin, 864 

Execute 命令

无涯教程可以使用execute命令从SED执行外部命令。它由e表示。下面给出了execute命令的语法。

[address1[,address2]]e [command]

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

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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 

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

1) A Storm of Swords, George R. R. Martin, 1216 
2) The Two Towers, J. R. R. Tolkien, 352 
Sun Sep  7 18:06:04 IST 2014 
3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
Sun Sep  7 18:06:04 IST 2014 
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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

date 
cal 
uname

在 e之后没有命令的情况下,SED会一一执行所有这些命令。下面的简单示例对此进行了说明。

[jerry]$sed 'e' commands.txt 

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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命令一样,execute命令也接受所有有效地址范围。

其他 命令

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

下面给出的是 N 命令的语法。

[address1[,address2]]N

让无涯教程打印以逗号分隔的书名及其作者列表。以下示例说明了这一点。

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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

像 p 命令一样,有一个 P 命令来打印由 N 命令。下面给出的是 P 命令的语法,与 p 命令相似。

[address1[,address2]]P 

在前面的示例中,看到 N 命令创建了以换行符分隔的书名及其作者列表。仅打印其中的第一部分,即仅打印书名。以下命令对此进行了说明。

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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

请注意,在没有 N 的情况下,它的行为与 p 命令相同。以下简单命令说明了这种情况。

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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中使用。

下面给出的是 v 命令的语法。

[address1[,address2]]v [version]

首先,找出SED的当前版本。

[jerry]$sed --version 

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

sed (GNU sed) 4.2.2 

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

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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

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

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

执行上述代码后,您将得到以下输出:

链接:https://www.learnfk.comhttps://www.learnfk.com/sed/sed-basic-commands.html

来源:LearnFk无涯教程网

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

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

技术教程推荐

从0开始学架构 -〔李运华〕

程序员进阶攻略 -〔胡峰〕

说透敏捷 -〔宋宁〕

Django快速开发实战 -〔吕召刚〕

商业思维案例笔记 -〔曹雄峰〕

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

手把手带你写一个MiniSpring -〔郭屹〕

后端工程师的高阶面经 -〔邓明〕

结构会议力 -〔李忠秋〕

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