5.2. 交互式编辑

5.2.1. 打印包含模式的行

这是你可以使用 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 打印了整个文件,但是包含搜索字符串的行被打印了两次。这不是我们想要的。为了只打印那些匹配我们模式的行,请使用-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. 删除包含模式的输入行

我们使用相同的示例文本文件。现在我们只想看到包含搜索字符串的行

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选项 'some/expression' file_to_process > sed_output_in_a_file

Tip更多示例
 

在你的机器的启动脚本中可以找到大量的 sed 示例,这些脚本通常位于/etc/init.d/etc/rc.d/init.d。更改到包含系统上的 initscripts 的目录,并发出以下命令

grepsed *