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

标签 linux sed

我想插入这个 block :

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

在此 block 之后:

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 {

直到现在我可以打印子网 block 直到“文件名”:

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     {\n\thardware ethernet c0:03:03:bc:30:fa;\n\t}\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;
            }
    }
}
  • 它首先会吞噬所有行以有效处理多行。
  • 它假定右花括号 没有出现在搜索目标 block 中 实现正则表达式中的最短匹配。

希望这对您有所帮助。

关于linux - 如何使用 linux sed 命令在第二个模式之后插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54132939/

相关文章:

linux - rsyslog 导入非标准日志

linux - 使用 shell 脚本将用户的输入 URL 附加到文件中的变量

linux - 使用 sed grep 文件名

linux - 将匹配模式的第一个字母大写

bash - 将连续的两行合并为一行(寻求一种也适用于 Windows 文件的解决方案)

linux - 如何处理名称为标志的文件

在 bash 循环中运行 R 函数

linux - 如何在 Windows 系统的 Linux 下使用 Firefox 进行测试?

c++ - QProcess,无法创建管道

regex - 按特定模式对文本文件中的列重新排序