ITEEDU

5.2. 交互编辑

5.2.1. 打印包含pattern的行

你可以用 grep 做不少事情,但是你不能用它来 “查找和替换” 。让我们开始。

这是我们的文本文件例子:

sandy ~> cat -n example
     1  This is the first line of an example text.
     2  It is a text with erors.
     3  Lots of erors.
     4  So much erors, all these erors are making me sick.
     5  This is a line not containing any errors.
     6  This is the last line.

sandy ~>

我们想让 sed 找到所有 “erors” 的行,我们使用 p 来得到结果:

sandy ~> sed  '/erors/p' example
This is the first line of an example text.
It is a text with erors.
It is a text with erors.
Lots of erors.
Lots of erors.
So much erors, all these erors are making me sick.
So much erors, all these erors are making me sick.
This is a line not containing any errors.
This is the last line.

sandy ~>

就像你看到的, sed 打印出了整个文件,但是包含搜索字符串的行被打印了2次。这不是我们想要的。为了只符合要求的行,使用 -n 选项:

sandy ~> sed -n '/erors/p' example
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.

sandy ~>

5.2.2. 删除包含pattern的输入行

我们使用同样的文本文件作为例子。现在我们只想看到那些 not 包含所要搜索字符串的行:

sandy ~> sed '/erors/d' example
This is the first line of an example text.
This is a line not containing any errors.
This is the last line.

sandy ~>

d 命令使得被排除的行不显示出来。

匹配以第一个模板开头的和第二个模板结尾的行应该写成这样:

sandy ~> sed -n '/^This.*errors.$/p' example
This is a line not containing any errors.

sandy ~>

5.2.3. 行的范围

这次我们想单独列出包含错误的行。在例子中它们是第2-4行。使用 d 命令来指定地址的范围:

sandy ~> sed '2,4d' example
This is the first line of an example text.
This is a line not containing any errors.
This is the last line.

sandy ~>

要打印出文件中以特定行开头的直到最后一行的,使用如下的命令:

sandy ~> sed '3,$d' example
This is the first line of an example text.
It is a text with erors.

sandy ~>

这个例子中只打印出例子里的头两行。

下面的命令打印出饱含 “a text”的第一次,直到下个包含 “a line” 的一行:

sandy ~> sed -n '/a text/,/This/p' example
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.
This is a line not containing any errors.

sandy ~>

5.2.4. 用sed来查找替换

在例子中,我们会搜索和替换错误来代替只选择(或者不选择)包含需要查找字符串的行。

sandy ~> sed 's/erors/errors/' example
This is the first line of an example text.
It is a text with errors.
Lots of errors.
So much errors, all these erors are making me sick.
This is a line not containing any errors.
This is the last line.

sandy ~>

就像你看到的,这并不完全就是我们想要得:在第4行,只有第一个待搜索字符串被替换了,左面还有一个“eror”没有被替换。使用 g 命令来使 sed 检查所有的行而不在搜索到第一个匹配字符串后就停止:

sandy ~> sed 's/erors/errors/g' example
This is the first line of an example text.
It is a text with errors.
Lots of errors.
So much errors, all these errors are making me sick.
This is a line not containing any errors.
This is the last line.

sandy ~>

要在文件的每一行开始处插入一个字符串,比如引用:

sandy ~> sed 's/^/> /' example
> This is the first line of an example text.
> It is a text with erors.
> Lots of erors.
> So much erors, all these erors are making me sick.
> This is a line not containing any errors.
> This is the last line.

sandy ~>

在每行的尾部插入字符串:

sandy ~> sed 's/$/EOL/' example
This is the first line of an example text.EOL
It is a text with erors.EOL
Lots of erors.EOL
So much erors, all these erors are making me sick.EOL
This is a line not containing any errors.EOL
This is the last line.EOL

sandy ~>

多个查找替换命令用单独选项 -e 来隔开:

sandy ~> sed -e 's/erors/errors/g' -e 's/last/final/g' example
This is the first line of an example text.
It is a text with errors.
Lots of errors.
So much errors, all these errors are making me sick.
This is a line not containing any errors.
This is the final line.

sandy ~>

记住 sed 默认把结果送到标准输出,比如你的终端窗口。如果你想把输出保存到文件,使用重定向:

sed option 'some/expression' file_to_process > sed_output_in_a_file

[提示] 更多例子

大量的 sed 例子可以在你机器的启动文件里找到,通常在 /etc/init.d 或者 /etc/rc.d/init.d。切换到包含初始化脚本的目录中然后输入日下命令:

grep sed *