我正在试图找到测试和插入行
示例
iface eth0:5 inet static
address 1.1.1.1
netmask 255.255.255.0
iface eth0:6 inet static
address 2.2.2.2
netmask 255.255.255.0替换
auto eth0:5
iface eth0:5 inet static
address 1.1.1.1
netmask 255.255.255.0
auto eth0:6
iface eth0:6 inet static
address 2.2.2.2
netmask 255.255.255.0发布于 2013-12-27 12:38:11
给你:
sed -e 's/iface \([^ ]*\) .*/auto \1\'$'\n''&/' file\([^ ]*\)将捕获接口的名称,以便\1可以在替换时使用,因此我们可以插入为auto \1,然后是换行符,然后是&,这意味着原始行。
如果要替换内联,可以使用-i标志,例如:
sed -ie 's/iface \([^ ]*\) .*/auto \1\'$'\n''&/' /etc/network/interfaces发布于 2013-12-27 12:43:06
sed应该可以工作,但是这个简单的awk也可以做到:
awk '/iface/ {print "auto",$2}1' file
auto eth0:5
iface eth0:5 inet static
address 1.1.1.1
netmask 255.255.255.0
auto eth0:6
iface eth0:6 inet static
address 2.2.2.2
netmask 255.255.255.0发布于 2013-12-27 12:39:14
如果您对perl没有意见的话:
perl -i -lane 'if(/^iface/){print "auto $F[1]\n$_"}else{print}' your_file请记住,这是一个内部替代。如果你不想换个地方,
perl -lane 'if(/^iface/){print "auto $F[1]\n$_"}else{print}' your_file您也可以使用awk:
awk '/^iface/{print "auto "$2}1' your_filehttps://stackoverflow.com/questions/20800708
复制相似问题