如何使用 linux sed 命令在第二个模式之后插入行



我想插入这个块:

host client3 {
    hardware ethernet c0:03:03:bc:30:fa;
}

在此块之后:

subnet 11.10.0.0 netmask 255.255.255.0 {
    range 11.10.1.2 11.10.1.254;
        group {
            filename "10M-5M-OKS2016NOV.cm";

行:filename "10M-5M-OKS2016NOV.cm";文件中多次出现。但只有一次在里面subnet 11.10.0.0 netmask 255.255.255.0 {

到目前为止,我可以打印子网块,直到"文件名":

sed -n -e :a -e '/subnet 11.10.0.0 netmask 255.255.255.0/,/}/{/filename "10M-5M-OKS2016NOV.cm";/!{$!{N;ba};};p;}' dhcpd.conf

但是当我尝试时:

sed -n -e :a -e '/subnet 11.10.0.0 netmask 255.255.255.0/,/}/{/filename "10M-5M-OKS2016NOV.cm";/!{$!{N;ba};};a thost client3     {nthardware ethernet c0:03:03:bc:30:fa;nt}n;}' dhcpd.conf

我得到:

sed:-e 表达式 #1,字符 0:不匹配的"{"

subnet 10.10.0.0 netmask 255.255.255.0 {
    range 10.10.0.2 10.10.0.254;
    group {
        filename "10M-5M-OKS2016NOV.cm";
        host client1 {
            hardware ethernet a0:b4:3d:bc:df:fa;
            }
        host client2 {
            hardware ethernet 90:6e:bb:ba:cd:d4;
            }
    }
}
subnet 11.10.0.0 netmask 255.255.255.0 {
    range 11.10.1.2 11.10.1.254;
    group {
        filename "10M-5M-OKS2016NOV.cm";
        host client1 {
            hardware ethernet c0:14:e3:bc:df:fa;
            }
        host client2 {
            hardware ethernet 90:6e:fb:ba:3d:04;
            }
    }
}
subnet 12.10.0.0 netmask 255.255.255.0 {
    range 12.10.2.2 12.10.2.254;
    group {
        filename "10M-5M-OKS2016NOV.cm";
        host client1 {
            hardware ethernet c0:a4:3d:bc:df:fa;
            }
        host client2 {
            hardware ethernet 90:6e:bb:ca:3d:04;
            }
    }
}

请尝试以下操作:

#!/bin/bash
# define newline and tab characters for replacement
NL=$'n'
NL="\$NL"
TAB=$'t'
TAB="\$TAB"
sed '
:l
N
$!b l
# first of all slurp all lines in the pattern space
# and perform the replacement over the lines
s/subnet 11.10.0.0 netmask 255.255.255.0[^}]*filename "10M-5M-OKS2016NOV.cm";/&'"$NL$TAB"'host client3 {'"$NL$TAB$TAB"'hardware ethernet c0:03:03:bc:30:fa;'"$NL$TAB"'}/g
' dhcpd.conf

它通过使用过帐的行作为dhcpd.conf产生以下输出,

subnet 10.10.0.0 netmask 255.255.255.0 {
    range 10.10.0.2 10.10.0.254;
    group {
        filename "10M-5M-OKS2016NOV.cm";
        host client1 {
            hardware ethernet a0:b4:3d:bc:df:fa;
            }
        host client2 {
            hardware ethernet 90:6e:bb:ba:cd:d4;
            }
    }
}
subnet 11.10.0.0 netmask 255.255.255.0 {
    range 11.10.1.2 11.10.1.254;
    group {
        filename "10M-5M-OKS2016NOV.cm";
        host client3 {
                hardware ethernet c0:03:03:bc:30:fa;
        }
        host client1 {
            hardware ethernet c0:14:e3:bc:df:fa;
            }
        host client2 {
            hardware ethernet 90:6e:fb:ba:3d:04;
            }
    }
}
subnet 12.10.0.0 netmask 255.255.255.0 {
    range 12.10.2.2 12.10.2.254;
    group {
        filename "10M-5M-OKS2016NOV.cm";
        host client1 {
            hardware ethernet c0:a4:3d:bc:df:fa;
            }
        host client2 {
            hardware ethernet 90:6e:bb:ca:3d:04;
            }
    }
}
  • 它最初首先对所有生产线进行啜食,以有效地处理多条生产线。
  • 它假定右大括号}未出现在搜索目标块中在正则表达式中实现最短的匹配。

希望这有帮助。

这可能对你有用(GNU sed):

sed '/subnet 11.10.0.0 netmask 255.255.255.0/{:a;n;/filename "10M-5M-OKS2016NOV.cm";/!ba;p;s/S.*/host client3 {/p;s//    hardware ethernet c0:03:03:bc:30:fa;/p;s//}/}' file

这将查找包含subnet 11.10.0.0 netmask 255.255.255.0的第一行,然后继续阅读,直到包含filename "10M-5M-OKS2016NOV.cm";的行。打印该行后,它将该行用作模板来格式化所需的详细信息。

另一种解决方案,使用预成型的插入文件:

cat <<! | sed '/subnet 11.10.0.0 netmask 255.255.255.0/!b;:a;n;/filename "10M-5M-OKS2016NOV.cm";/!ba;r /dev/stdin' file
        host client3 {
            hardware ethernet c0:03:03:bc:30:fa;
            }
!

sed作为流编辑器非常有用,这意味着可以多次处理相同的操作。在这里,您只想插入一次文本组。使用ed,这将更简单(更具可读性和可维护性):

ed dhcpd.conf <<EOF
/subnet 11.10.0.0/
/filename/
a
        host client3 {
            hardware ethernet c0:03:03:bc:30:fa;
            }
.
w
q
EOF

注意:ed是一个文件编辑器。这意味着 dhcpd.conf 文件将被上述脚本更改。如果出现问题,请确保有备份...

最新更新