我想在字符串匹配+2行之后添加一个新行。
这是我的文件:
allow-hotplug eth0
auto eth0
iface eth0 inet static
address 172.16.2.245
netmask 255.255.254.0
gateway 192.168.1.1
allow-hotplug eth1
#auto eth1
iface eth1 inet static
address 192.168.0.240
netmask 255.255.255.0
iface eth2 inet static
address 192.168.2.240
netmask 255.255.255.0我想在找到'iface eth1‘+2行之后添加'gateway 192.168.1.1’。
示例:执行sed命令后需要获取的内容
allow-hotplug eth0
auto eth0
iface eth0 inet static
address 172.16.2.245
netmask 255.255.254.0
gateway 172.16.2.254
allow-hotplug eth1
#auto eth1
iface eth1 inet static
address 192.168.0.240
netmask 255.255.255.0
gateway 192.168.1.1
iface eth2 inet static
address 192.168.2.240
netmask 255.255.255.0我知道如何找到并移动2行后,在特定字符串后追加行,等等,但不结合这2个操作。斯蒂芬
发布于 2013-04-25 21:00:49
这似乎起作用了:
sed '/^iface eth1/{N;N;s/$/\ngateway 192.168.1.1/}' input.txt将-i选项添加到sed以将结果保存回input.txt。
发布于 2013-04-25 21:04:13
使用sed的一种方式
sed '
/iface eth1/ {
n
n
a\gateway 192.168.1.1
}' file发布于 2013-04-25 21:10:32
您要求使用' sed‘,但'Kent’使用的是'awk',这里有一个sed脚本,它可以为您的示例执行您想要的操作。更一般地说,sed脚本的第1行可以包含您想要的任何字符串,sed脚本的第5行可以包含您想要的任何字符串。将以下脚本放入一个文件中,比如x.sed,不要添加任何空格或制表符。
/iface eth1/{
n
n
a\
gateway 192.168.1.1
}然后在命令行上像这样运行它。
sed -f x.sed "myinputfile" > "myoutputfile"https://stackoverflow.com/questions/16214828
复制相似问题