编辑文本文件和终端输出是管理 Linux 机器的人的日常工作。 像 sed 这样的命令行实用程序允许用户直接从终端窗口修改和更改文本文件的内容。
在本文中,我们将详细讨论 sed 命令,以及一些展示 Linux 中 sed 实用程序强大功能的基本示例。
什么是 sed 命令?
sed 命令,它是 流编辑器, 是一个命令行工具,允许 Linux 用户对文件和终端输出执行基于文本的操作。 使用 sed,用户可以查找和替换文本中的特定单词、显示输出的特定部分以及编辑文本文件而无需打开它们。
sed 命令支持的三个基本操作是:
- 插入
- 删除
- 替换(查找和替换)
高级用户还可以使用 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 命令可能会派上用场。
输出文件第 3 行和第 5 行之间的内容 文本文件.txt:
sed -n '3,5p' textfile.txt
这 -n flag 防止 sed 在每个循环结束时显示模式空间。 您还可以使用 – 安静的 和 – 沉默的 选项而不是 -n. 这 p 论据代表 打印 并用于向用户显示匹配的行。
执行上述命令 example 文件产生以下输出。
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 flag 从输出中删除匹配的字符串并显示其余内容。
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. 在行间插入空格
如果出于任何原因要在文本文件的每一行之间插入空行,请使用 G 默认 sed 命令的参数。
sed G textfile.txt
要在输出中插入多个空行,请传递多个 G 由 分号 (;) 特点。
sed 'G;G' textfile.txt
4.更换一个 Word 在文本文件中
如果您想用其他单词替换每个出现的特定单词,请使用 s 和 G 命令的参数。 使用 sed 命令替换单词的基本语法是:
sed s/originalword/replaceword/g filename
使用上述语法,您可以替换单词 惊人的 和 极好的 在文件中 文本文件.txt:
sed s/amazing/super/g textfile.txt
这 s 参数表示 替代 和 G 命令用于将匹配的内容替换为指定的替换内容。
要将第二次出现的单词替换为 sed,请将一个数字传递给 G 争论。 在这种情况下:
sed s/amazing/super/g2 textfile.txt
如果要在替换单词时忽略字符大小写,请使用 吉 代替 G, 在哪里 一世 代表 忽视 案子。
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 命令仅当在该行中找到给定的匹配项时才替换单词。 为了 example替换单词 一种 和 一个 如果这个词 橙 存在于该行中:
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.
注意这个词 一种 在行中 这是一个苹果 没有被替换,因为系统没有找到这个词 橙 在里面。
8. 使用正则表达式替换单词
对于知道如何使用正则表达式的人来说,使用 sed 命令对字符串执行操作会变得容易得多。 您可以实现正则表达式以增强命令的功能。
替换所有出现的单词 惊人的 或者 惊人的 和 极好的:
sed -e 's/[Aa]mazing/super/g' textfile.txt
同样,您也可以使用 sed 命令使用高级正则表达式来执行特定操作。
9. 使用其他命令管道 sed
您也可以将 sed 与其他 Linux 命令链接起来。 为了 example,你可以通过管道 lspci 带有 sed 的命令在输出中的行之间添加空格。
lspci | sed G
替换输出中的特定单词 ip路由显示 命令:
ip route show | sed s/src/source/g
上述命令替换了单词 来源 代替原来的词 源代码.
10.编辑和备份原始文件
当您使用系统文件时,在进行更改时备份原始文件很重要。 这将帮助您在出现问题时恢复更改。
要使用 sed 备份原始文件,请使用 -一世 命令中的标志。
sed -i'.backup' 's/amazing/super/g' textfile.txt
将使用名称创建一个新文件 文本文件.txt.备份. 您可以使用以下命令检查两个文件是否不同 差异 命令。
diff textfile.txt textfile.txt.backup
在 Linux 中使用 sed 编辑字符串
有时,当您在终端上处理文本文件时,必须格式化和编辑输出以提高可读性。 Sed 和 awk 是 Linux 中的命令行实用程序,它们允许用户通过将数据分成单独的行来有效地处理文本文件。
许多用户很难记住 sed 命令的参数和标志,因为其中有很多可供使用。 了解如何获取任何 Linux 命令的命令行手册将帮助您轻松摆脱这种情况。