Sed命令及示例

在这里插入图片描述

Unix/Linux中的SED(流编辑器)命令是一个强大的工具,用于处理和操作文件中的文本。它无需打开文件编辑器即可执行多种操作,如搜索、查找替换、插入、删除等。这使得它成为管理和编辑文本的高效工具,特别是在处理大文件或在脚本中自动化任务时。

• SED是一个强大的文本流编辑器。可以执行插入、删除、搜索和替换(substitution)操作。

• Unix中的SED命令支持正则表达式,这使其能够执行复杂的模式匹配。

语法:

sed [OPTIONS] 'SCRIPT' [INPUTFILE...]

其中,

‘OPTIONS’:这些是可选标志,用于修改sed命令的行为。

‘SCRIPT’:定义要在输入文件上执行的命令或命令序列。

‘INPUTFILE’:一个或多个要处理的输入文件。

SED中常用选项

选项描述
-i在不打印到控制台的情况下原地编辑文件(覆盖文件)。
-n抑制自动打印行。
-e允许执行多个命令。
-f从文件而不是命令行读取sed命令。
-r启用扩展正则表达式。

SED命令用法的实际示例

考虑以下文本文件作为输入。

$cat > geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

1.示例命令

替换或替换字符串:SED命令主要用于替换文件中的文本。以下简单的sed命令将文件中的单词“unix”替换为“linux”。

$sed 's/unix/linux/' geekfile.txt

输出:

linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

这里的“s”指定替换操作。“/”是分隔符。“unix”是搜索模式,“linux”是替换字符串。默认情况下,sed命令仅替换每行中模式的第一个出现,而不会替换该行中的第二个、第三个……出现。

2.替换行中模式的第n个出现

使用‘/1’、‘/2’等标志来替换行中模式的第一个、第二个出现。以下命令将行中单词“unix”的第二个出现替换为“linux”。

$sed 's/unix/linux/2' geekfile.txt

输出:

unix is great os. linux is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.linux is a multiuser os.Learn unix .unix is a powerful.

3.替换行中模式的所有出现

替换标志/g(全局替换)指定sed命令替换行中字符串的所有出现。

$sed 's/unix/linux/g' geekfile.txt

输出:

linux is great os. linux is opensource. linux is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.

4.从行中模式的第n个出现替换到所有出现

使用/1、/2等和/g的组合来替换行中从第n个出现开始的所有模式。以下sed命令将行中第三个、第四个、第五个……“unix”单词替换为“linux”单词。

$sed 's/unix/linux/3g' geekfile.txt

输出:

unix is great os. unix is opensource. linux is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn linux .linux is a powerful.

5.用括号括起每个单词的第一个字符

这个sed示例打印每个单词的第一个字符用括号括起来。

$ echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'

输出:

(W)elcome (T)o (T)he (G)eek (S)tuff

6.在特定行号上替换字符串

您可以限制sed命令仅在特定行号上替换字符串。一个示例是

$sed '3 s/unix/linux/' geekfile.txt

输出:

unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

上述sed命令仅替换第三行上的字符串。

7.使用/p标志重复替换的行

/p打印标志在终端上两次打印替换的行。如果一行没有搜索模式且未被替换,则/p仅打印该行一次。

$sed 's/unix/linux/p' geekfile.txt

输出:

linux is great os. unix is opensource. unix is free os.
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

8.仅打印替换的行

使用-n选项与/p打印标志一起仅显示替换的行。在这里,-n选项抑制了由/p标志生成的重复行,并仅打印替换的行一次。

$sed -n 's/unix/linux/p' geekfile.txt

输出:

linux is great os. unix is opensource. unix is free os.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

如果您单独使用-n而不使用/p,则sed不会打印任何内容。

9.在一系列行上替换字符串

您可以为sed命令指定一系列行号以替换字符串。

$sed '1,3 s/unix/linux/' geekfile.txt

输出:

linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

这里sed命令替换从1到3的行范围。另一个示例是

$sed '2,$ s/unix/linux/' geekfile.txt

输出:

unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful

这里表示文件中的最后一行。因此sed命令替换从第二行到文件中最后一行的文本。

10.删除特定文件中的行

SED命令还可以用于删除特定文件中的行。SED命令用于执行删除操作,甚至无需打开文件。

示例:

1.删除特定行,例如这里的第n行

语法:
$ sed 'nd' filename.txt
示例:
$ sed '5d' filename.txt

2.删除最后一行

语法:
$ sed '$d' filename.txt

3.删除从x到y的行范围

语法:
$ sed 'x,yd' filename.txt
示例:
$ sed '3,6d' filename.txt

4.删除从第n行到最后一行

语法:
$ sed 'nth,$d' filename.txt
示例:
$ sed '12,$d' filename.txt

5.删除匹配模式的行

语法:
$ sed '/pattern/d' filename.txt
示例:
$ sed '/abc/d' filename.txt

更多内容:Sed命令及示例2

结论

Linux/Unix中的sed命令是一个多功能且强大的工具,用于自动化文本编辑任务,从简单的查找替换操作到更复杂的模式匹配。无论您是处理单个文件还是需要在脚本中处理多个文件,sed都为文本操作提供了一个高效的解决方案。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值