sed使用简单,我们可以提供sed命令直接在命令行或具有sed命令的文本文件的形式。本教程讲解调用sed的例子,有这两种方法:
Sed 命令行
以下是我们可以指定单引号在命令行sed命令的格式如下:
sed [-n] [-e] 'command(s)' files
例子
考虑一下我们有一个文本文件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文件的完整显示内容如下:
[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) A Game of Thrones, George R. R. Martin, 864
现在,我们从上述文件中显示将看到sed的delete命令删除某些行。让我们删除了第一,第二和第五行。在这里,要删除给定的三行,我们已经指定了三个单独的命令带有-e选项。
[jerry]$ sed -e '1d' -e '2d' -e '5d' books.txt
执行上面的代码,会得到如下结果:
3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 6) A Game of Thrones, George R. R. Martin, 864
sed脚本文件
下面是第二种形式,我们可以提供一个sed脚本文件sed命令:
sed [-n] -f scriptfile files
首先,创建一个包含在一个单独的行的文本commands.txt文件,每次一行为每个sed命令,如下图所示:
1d 2d 5d
现在,我们可以指示sed从文本文件中读取指令和执行操作。这里,我们实现相同的结果,如图在上述的例子。
[jerry]$ sed -f commands.txt books.txt
执行上面的代码,会得到如下结果:
3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 6) A Game of Thrones,George R. R. Martin, 864
sed标准选项
sed支持可从命令行提供下列标准选择。
-n 选项
这是模式缓冲区的缺省打印选项。 GNU sed解释器提供--quiet,--silent选项作为 -n选项的替代。
例如,下面 sed 命令不显示任何输出:
[jerry]$ sed -n '' quote.txt
-e 选项
-e选项的编辑选项。通过使用此选项,可以指定多个命令。例如,下面 sed 命令打印每行两次:
[jerry]$ sed -e '' -e 'p' quote.txt
执行上面的代码,会得到如下结果:
There is only one thing that makes a dream impossible to achieve: the fear of failure. There is only one thing that makes a dream impossible to achieve: the fear of failure. - Paulo Coelho, The Alchemist - Paulo Coelho, The Alchemist
-f 选项
-f选项是用来提供包含sed命令的文件。例如,我们可以按如下方法通过文件指定一个打印命令:
[jerry]$ echo "p" > commands.txt [jerry]$ sed -n -f commands quote.txt
执行上面的代码,会得到如下结果:
There is only one thing that makes a dream impossible to achieve: the fear of failure. - Paulo Coelho, The Alchemist
像其他的编程语言,sed还提供了一个循环和分支工具来控制程序的执行流程。本教程将探讨如何使用sed的循环和分支。
sed循环的工作原理类似于现代编程语言中的goto语句。 sed可以跳转到标记标签的行并继续执行下面提供该标签的剩余命令。
以下是对在sed定义一个标签的语法。在这里,冒号后的名称(:)暗示的标签名称。
:label :start :end :up
要跳转到一个特定的标签,我们可以使用 b 命令后面跟标签名称。如果标签的名称省略,则 sed 跳转到 sed 文件的末尾。
考虑一下我们有一个待处理文本文件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
下面的例子是连接书名,并在一行用逗号分隔作者姓名。然后,它会搜索模式“Paulo”。如果能够匹配,它打印一个连字符(- )在该行的前面,否则跳转到打印行打印标签。
[jerry]$ sed -n ' h;n;H;x s/\n/, / /Paulo/!b Print s/^/- / :Print 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读入模式缓冲区第一行即书名和保持缓冲区保持为空。后执行-h命令模式缓冲区被复制到保留缓冲区。现在,这两个缓冲区包含了本书即标题. A Storm of Swords. 接下来n命令打印当前的模式缓冲区(在本例中没有打印出来,因为-n选项),清除当前图形缓冲区读取输入的下一行。现在模式缓冲区包含George R. R. Martin。
-
第三个命令跳到仅当模式不匹配,否则取代是由第四指令执行的标签Print。
-
:Print 仅仅是一个标签名,p是打印命令。
为了提高可读性,每个sed命令被放置在一个单独的行。然而,人们可以选择将所有命令在一行中,如下所示:
[jerry]$ sed -n 'h;n;H;x;s/\n/, /;/Paulo/!b Print; s/^/- /; :Print;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
可以用t命令创建分支。 t 命令跳转到标签,只有在以前的替换命令是成功的。让我们以前面的章节同样的例子,但不是打印一个连字符(- ),现在我们印刷四连字符。下面的例子演示了 t 命令的用法。
[jerry]$ sed -n ' h;n;H;x s/\n/, / :Loop /Paulo/s/^/-/ /----/!t Loop 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
我们已经讨论了在前面的章节中的第一个命令。第三个命令定义一个标签循环。第四命令上前置的连字符( - ),如果该行包含字符串“Paulo”和t命令重复这一过程,直到有四个连字符位于行的开头。
为了提高可读性,每个 sed 命令写在一个单独的行。否则,我们可以写一行一个 sed 如下:
[jerry]$ sed -n 'h;n;H;x; s/\n/, /; :Loop;/Paulo/s/^/-/; /----/!t Loop; 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
from: http://www.yiibai.com/sed/sed_useful_recipes.html