打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
通过 20 个实例掌握 Linux Sed 命令
userphoto

2023.02.04 广东

关注

SED 命令或 流编辑器是 Linux / Unix 系统提供的非常强大的实用程序。它主要用于文本替换,查找和替换,但也可以执行其他文本操作,例如 插入,删除,搜索 等。使用 SED,我们可以编辑完整的文件而无需打开它。SED 还支持使用正则表达式,这使得 SED 成为更强大的测试操作工具。

基本语法如下:

sed OPTIONS… [SCRIPT] [INPUTFILE…]

(1) 显示文件的部分文本

使用 sed,可以只查看文件的一部分,而不是查看整个文件,示例如下:[linuxtechi@localhost ~]$ sed -n 22,29p testfile.txt

本例子,选项 n 将抑制整个文件的打印,选项 p 将只打印 22-29 行

(2) 显示除某些行之外的所有行

使用选项 d,显示除 22-29 行之外的所有行

[linuxtechi@localhost ~]$ sed 22,29d testfile.txt

(3) 显示从第 n 行开始的每 m 行

显示从第 2 行或任何其他行开始的每 3 行内容,使用以下命令[linuxtechi@localhost ~]$ sed -n '2~3p' file.txt

(4) 删除一行

其中 N 是行号,选项 d 将删除提到的行号,示例如下:

[linuxtechi@localhost ~]$ sed Nd testfile.txt

若要删除文件的最后一行,请使用如下命令:[linuxtechi@localhost ~]$ sed $d testfile.txt

(5) 删除一系列行

从 testfile.txt 文件中删除 29-34 行

[linuxtechi@localhost ~]$ sed '29,34d' testfile.txt

(6) 删除范围以外的行

从 testfile.txt 文件中删除 29-34 之外的行[linuxtechi@localhost ~]$ sed '29,34!d' testfile.txt

(7) 添加空白行 / 空格

使用选项 G, 可以在每个非空行之后添加一个空行

[linuxtechi@localhost ~]$ sed G testfile.txt

(8) 查找和替换(首次替换)

使用 s 选项,将搜索 danger,并将其替换为 saftey,执行首次匹配。[linuxtechi@localhost ~]$ sed 's/danger/safety/' testfile.txt

(9) 查找和替换(全局替换)

为了完全替换文件中的所以单词,我们将使用带有 s 的选项 g

[linuxtechi@localhost ~]$ sed 's/danger/safety/g' testfile.txt

(10) 替换第 n 次出现的字符串模式

还可以在第 n 次出现时替换字符串,比如只有在第二次出现时才用 danger 替换 safety,依然是首次替换模式[linuxtechi@localhost ~]$ sed 's/danger/safety/2' testfile.txt

为了完全替换第 2 次出现的所有单词,我们将使用带有 s 的选项 g,完全替换模式

[linuxtechi@localhost ~]$ sed 's/danger/safety/2g' testfile.txt

(11) 替换特定行上的字符串

只替换文件第 4 行的字符串[linuxtechi@localhost ~]$ sed '4 s/danger/safety/' testfile.txt

替换文件第 4-9 行的字符串

[linuxtechi@localhost ~]$ sed '4,9 s/danger/safety/' testfile.txt

(12) 在匹配搜索之后 / 之前添加一行

使用选项 a, 在每个模式匹配之后添加新行[linuxtechi@localhost ~]$ sed '/danger/a 'This is new line with text after match'' testfile.txt

使用选项 i, 在每个模式匹配之前添加新行

[linuxtechi@localhost ~]$ sed '/danger/i 'This is new line with text before match' ' testfile.txt

(13) 用匹配的模式更改整行

使用 c 选项,当匹配时,正行都会被新内容替换,示例如下:[linuxtechi@localhost ~]$ sed '/danger/c 'This will be the new line' ' testfile.txt

到目前为止,我们只使用 sed 的简单表达式,现在我们将讨论 sed 与 regex 的一些高级用法

(14) 运行多个 sed 命令

如果需要执行多个 sed 表达式,可以使用选项 e 将 sed 命令链接起来

[linuxtechi@localhost ~]$ sed -e 's/danger/safety/g' -e 's/hate/love/' testfile.txt

(15) 在编辑文件之前进行备份

编辑之前创建文件的备份副本,请使用选项 -i.bak[linuxtechi@localhost ~]$ sed -i.bak -e 's/danger/safety/g'  testfile.txt

这将创建扩展名为.bak 的文件的备份副本,你也可以使用其他扩展,例如 -i.backup

(16) 删除以模式开头和结尾的文件行

删除以特定字符串开始并以另一个字符串结束的行,示例如下:

[linuxtechi@localhost ~]$ sed -e 's/^danger.*stops$//g' testfile.txt

(17) 附加行

使用 sed & regex 在每行之前添加一些内容,示例如下:[linuxtechi@localhost ~]$ sed -e 's/.*/testing sed &/' testfile.txt

(18) 删除所有注释行和空行

要删除所有注释行,即带有 # 和所有空行的行,使用如下命令

[linuxtechi@localhost ~]$ sed -e 's/#.*//;/^$/d' testfile.txt

只删除注释行,使用如下命令:[linuxtechi@localhost ~]$ sed -e 's/#.*//' testfile.txt

(19) 从 /etc/passwd 文件获取所有用户名

要获取 /etc/passwd 文件的所有用户名列表,使用如下命令:

[linuxtechi@localhost ~]$ sed 's/\([^:]*\).*/\1/' /etc/passwd

(20) 防止覆盖系统链接

sed -i 命令已经被用来删除系统链接,并只创建常规文件来代替链接文件。因此,为了避免这种情况并防止 sed -i 破坏链接,请在执行命令时使用 follow-symklinks 选项。

假设我们想在 CentOS 或 RHEL 服务器上禁用 SELinux[linuxtechi@localhost ~]# sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux命令大全(排版清晰持续更新非常详细)
grep和sed配合替换文件中的字串
利用sed命令插入文本
opentextfile用法
在 Linux 下 9 个有用的 touch 命令示例 | Linux 中国
8 个实用的 Linux netcat 命令示例
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服