这 10 个 Sed 示例将使您成为 Linux 高级用户这 10 个 Sed 示例将使您成为 Linux 高级用户这 10 个 Sed 示例将使您成为 Linux 高级用户这 10 个 Sed 示例将使您成为 Linux 高级用户
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

这 10 个 Sed 示例将使您成为 Linux 高级用户

想成为 Linux 高级用户吗?掌握 sed 会有帮助。从这 10 个 sed 示例中学习。

编辑文本文件和终端输出是管理 Linux 机器的人员的日常工作。 sed 等命令行实用程序允许用户直接从终端窗口修改和更改文本文件的内容。

在本文中,我们将详细讨论 sed 命令,并提供一些基本示例来演示 Linux 中 sed 实用程序的强大功能。

sed 命令是什么?

sed 命令是Stream Editor 的缩写,是一个命令行工具,允许 Linux 用户对文件和终端输出执行基于文本的操作。使用 sed,用户可以查找和替换文本中的特定单词、显示输出的特定部分以及编辑文本文件而无需打开它们。

sed 命令支持的三个基本操作是:

  1. 插入

  2. 删除

  3. 替换(查找和替换)

高级用户还可以使用 sed 命令实现正则表达式,以更有效地编辑文本流。

该命令的基本语法是:

sed [options] [pattern] [filepath]

...其中选项是命令的各种功能,模式是正则表达式或要匹配的脚本,文件路径是包含文本的文本文件的路径。

Linux sed 命令的 10 个示例

如果您计划成为一名普通 Linux 用户,了解如何编辑文件、搜索和替换特定单词以及过滤终端输出可能对您有用。本节介绍 sed 命令的一些示例,它们肯定会让您成为 Linux 高级用户。

我们将在帖子中使用以下文本文件进行演示。

This is a demo text file.
It is an amazing file that will help us all.
The sed command is also great for stream editing.
Want to learn how to use the command?
This is another line in the file.
This is the third general line in the file.
This file is named as textfile.
This is a apple.
This is a orange.

1. 查看一系列行

Linux 命令(例如 head 和 tail)输出文本文件的第一行或最后十行。但是如果您想获取文件中两个特定行之间的内容该怎么办?在这种情况下,sed 命令可能会派上用场。

输出文件 textfile.txt 第 3 行和第 5 行之间的内容:

sed -n '3,5p' textfile.txt

-n 标志阻止 sed 在每个循环结束时显示模式空间。您还可以使用--quiet和--silent选项来代替-n。 p 参数代表 print,用于向用户显示匹配的行。

对示例文件执行上述命令会产生以下输出。

The sed command is also great for stream editing.
Want to learn how to use the command?
This is another line in the file.

要输出除指定范围之外的整个文件内容,请在命令中使用 d 标志而不是 p:

sed '3,5d' textfile.txt

d 标志从输出中删除匹配的字符串并显示其余内容。

This is a demo text file.
It is an amazing file that will help us all.
This is the third general line in the file.
This file is named as textfile.
This is a apple.
This is a orange.

2. 显示不连续的行

要打印文件中多个范围之间的非连续行:

sed -n -e '1,2p' -e '5,6p' textfile.txt

输出 :

This is a demo text file.
It is an amazing file that will help us all.
This is another line in the file.
This is the third general line in the file.

-e 标志有助于使用单个命令执行多个操作。

3. 在行之间插入空格

如果出于某种原因您想要在文本文件中的每行之间插入空行,请在默认 sed 命令中使用 G 参数。

sed G textfile.txt

要在输出中插入多个空行,请传递多个由分号 (;) 字符分隔的G 参数。

sed 'G;G' textfile.txt

4. 替换文本文件中的单词

如果要将特定单词的每次出现替换为其他单词,请在命令中使用 s 和 g 参数。使用 sed 命令替换单词的基本语法是:

sed s/originalword/replaceword/g filename

使用上述语法,您可以在文件 textfile.txt 中将单词 amazing 替换为 super:

sed s/amazing/super/g textfile.txt

s 参数表示替换,g 命令用于将匹配的内容替换为指定的替换内容。

要将第二次出现的单词替换为 sed,请将数字传递给 g 参数。在这种情况下:

sed s/amazing/super/g2 textfile.txt

如果您想在替换单词时忽略字符大小写,请使用 gi 而不是 g,其中 i 代表忽略案件。

sed s/Amazing/super/gi textfile.txt

5. 替换某个范围内的单词

您还可以替换特定范围内的单词。

sed '2,5s/amazing/super/g' textfile.txt

6. 一次执行多个替换

如果您想同时执行两个或多个替换,只需用分号 (;) 字符分隔命令即可。

sed 's/amazing/super/g;s/command/utility/gi' textfile.txt

系统将显示以下输出。

This is a demo text file.
It is an super file that will help us all.
The sed utility is also great for stream editing.
Want to learn how to use the utility?
This is another line in the file.
This is the third general line in the file.
This file is named as textfile.
This is a apple.
This is a orange.

7.仅在找到匹配项时才替换单词

您还可以使用 sed 命令仅在行中找到给定匹配项时才替换单词。例如,如果行中存在单词 orange,则要将单词 a 替换为 an:

sed -e '/orange/ s/a/an/g' textfile.txt

发出上述命令将输出:

This is a demo text file.
It is an super file that will help us all.
The sed utility is also great for stream editing.
Want to learn how to use the utility?
This is another line in the file.
This is the third general line in the file.
This file is named as textfile.
This is a apple.
This is an orange.

请注意,This is a apple 行中的单词 a 未被替换,因为系统在其中找不到单词 orange。

8. 使用正则表达式替换单词

对于那些知道如何使用正则表达式的人来说,使用 sed 命令对字符串执行操作变得容易得多。您可以实施正则表达式来增强命令的功能。

要将所有出现的单词 Amazing 或 amazing 替换为 super:

sed -e 's/[Aa]mazing/super/g' textfile.txt

同样,您也可以使用高级正则表达式使用 sed 命令执行特定操作。

9. 通过管道 sed 与其他命令

您也可以将 sed 与其他 Linux 命令链接起来。例如,您可以将lspci命令与 sed 进行管道传输,以在输出中的行之间添加空格。

lspci | sed G

要替换 ip route show 命令输出中的特定单词:

ip route show | sed s/src/source/g

上述命令用单词 source 代替原来的单词 src。

10. 编辑并备份原文件

当您使用系统文件时,在进行更改时备份原始文件非常重要。这将帮助您在出现问题时恢复更改。

要使用 sed 备份原始文件,请在命令中使用 -i 标志。

sed -i'.backup' 's/amazing/super/g' textfile.txt

将创建一个名为 textfile.txt.backup 的新文件。您可以使用 diff 命令检查这两个文件是否不同。

diff textfile.txt textfile.txt.backup

在 Linux 中使用 sed 编辑字符串

有时,当您在终端上处理文本文件时,必须格式化和编辑输出以获得更好的可读性。 sed 和 awk 是 Linux 中的命令行实用程序,允许用户通过将数据分成单独的行来有效地处理文本文件。

许多用户很难记住 sed 命令的参数和标志,因为有很多可供使用的参数和标志。了解如何获取任何 Linux 命令的命令行手册将帮助您轻松摆脱这种情况。

©2015-2025 艾丽卡 support@alaica.com