‘sed’:如何在字符串匹配2行后添加新行

‘sed’:如何在字符串匹配2行后添加新行,第1张

概述我想在字符串匹配2行之后添加一个新行. 这是我的档案: allow-hotplug eth0auto eth0iface eth0 inet staticaddress 172.16.2.245netmask 255.255.254.0gateway 192.168.1.1allow-hotplug eth1#auto eth1iface eth1 inet staticadd 我想在字符串匹配2行之后添加一个新行.

这是我的档案:

allow-hotplug eth0auto eth0iface eth0 inet staticaddress 172.16.2.245netmask 255.255.254.0gateway 192.168.1.1allow-hotplug eth1#auto eth1iface eth1 inet staticaddress 192.168.0.240netmask 255.255.255.0iface eth2 inet staticaddress 192.168.2.240netmask 255.255.255.0

我想在找到’iface eth1’2行后添加’gateway 192.168.1.1′.

例子:执行sed命令后我需要得到什么

allow-hotplug eth0auto eth0iface eth0 inet staticaddress 172.16.2.245netmask 255.255.254.0gateway 172.16.2.254allow-hotplug eth1#auto eth1iface eth1 inet staticaddress 192.168.0.240netmask 255.255.255.0gateway 192.168.1.1iface eth2 inet staticaddress 192.168.2.240netmask 255.255.255.0

我知道如何找到并移动2行后,在特定字符串之后附加行等,但不能合并这2个 *** 作.
斯蒂芬

解决方法 这似乎有效:

sed '/^iface eth1/{N;N;s/$/\ngateway 192.168.1.1/}' input.txt

将-i选项添加到sed以将结果保存回input.txt.

总结

以上是内存溢出为你收集整理的‘sed’:如何在字符串匹配2行后添加新行全部内容,希望文章能够帮你解决‘sed’:如何在字符串匹配2行后添加新行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1030145.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存