[英]How to insert lines after second pattern with linux sed command
I want to insert this block: 我想插入这个块:
host client3 {
hardware ethernet c0:03:03:bc:30:fa;
}
after this 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";
The line: filename "10M-5M-OKS2016NOV.cm";
行:
filename "10M-5M-OKS2016NOV.cm";
apears multiple times in the file. apears在文件中多次。 But only once inside
subnet 11.10.0.0 netmask 255.255.255.0 {
但只有一次内部
subnet 11.10.0.0 netmask 255.255.255.0 {
Until now I can print the subnet block until the "filename": 到目前为止,我可以打印子网块,直到“文件名”:
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
but when I try: 但是当我尝试:
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
I get: 我明白了:
sed: -e expression #1, char 0: unmatched `{' sed:-e表达式#1,char 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;
}
}
}
Please try something like: 请尝试以下方法:
#!/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
It yields the following output by using the posted lines as 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;
}
}
}
}
does not appear in the search target block to achieve the shortest match in regex. }
没有出现在搜索目标块中以实现正则表达式中的最短匹配。 Hope this helps. 希望这可以帮助。
sed
is great as a stream editor, that means to process multiple times the same actions. sed
非常适合作为流编辑器,这意味着可以多次处理相同的操作。 Here you just want to insert once a bloc of text. 在这里,您只想插入一个文本块。 That would be much simpler (more readable and maintenable) with
ed
: 使用
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
Beware: ed is a file editor. 注意:ed是一个文件编辑器。 That means that the dhcpd.conf file will be changed by the above script.
这意味着上面的脚本将更改dhcpd.conf文件。 Make sure to have a backup if things goes wrong...
如果出现问题,请确保备份...
This might work for you (GNU sed): 这可能适合你(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
This finds the first line containing subnet 11.10.0.0 netmask 255.255.255.0
and then reads on further until the line containing filename "10M-5M-OKS2016NOV.cm";
这将找到包含
subnet 11.10.0.0 netmask 255.255.255.0
的第一行,然后继续读取,直到包含filename "10M-5M-OKS2016NOV.cm";
. 。 After printing that line it uses the line as a template to format the required detail.
在打印该行之后,它使用该行作为模板来格式化所需的细节。
Another solution, using a preformed insertion file: 另一种解决方案,使用预先形成的插入文件:
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;
}
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.