我想编写一个程序来将系统IP地址从dhcp / static更改为static。我原来的文件内容将是这个
some initial text
[ipv6]
method=auto
[ipv4]
method=static
dns=8.8.8.8;
addresses1=192.168.1.10;24;192.168.1.1;
或者可能是这样,
some initial text
[ipv6]
method=auto
[ipv4]
method=auto
现在,我想在[ipv4]模式之后进行更改,以便在编辑我的文件之后应该看起来像这样
some initial text
[ipv6]
method=auto
[ipv4]
method=static
dns=8.8.8.8;4.4.4.4;
addresses1=192.168.1.20;24;192.168.1.1;
答案 0 :(得分:0)
这是一个Perl解决方案。它将记录分隔符设置为2个换行符,因此将读取段落。如果段落包含[ipv4]
,请用替换替换输入。将data
替换为您的文件名,或使用"$@"
替换为shell脚本。如果要覆盖文件,请将-i
添加为Perl选项(在您对其进行测试之前不要这样做。)
perl -pe '
my $replacement = qq"[ipv4]\n" .
"method=static\n" .
"dns=8.8.8.8;4.4.4.4;\n" .
"addresses1=192.168.1.20;24;192.168.1.1;\n\n";
$/ = "\n\n";
$_ = $replacement if (m/^\[ipv4\]$/m);' data
有许多替代方法可以编写代码;除了&#34之外我没有提出任何声明;它适用于我"。
将问题的文本作为输入,输出为:
I want to write a program to change systems ip address from dhcp/static to static only. my original file's contents will be either this
some initial text
[ipv6]
method=auto
[ipv4]
method=static
dns=8.8.8.8;4.4.4.4;
addresses1=192.168.1.20;24;192.168.1.1;
or it may be like this,
some initial text
[ipv6]
method=auto
[ipv4]
method=static
dns=8.8.8.8;4.4.4.4;
addresses1=192.168.1.20;24;192.168.1.1;
Now, i want to change after [ipv4] pattern, so that after editing my file should looks like this
some initial text
[ipv6]
method=auto
[ipv4]
method=static
dns=8.8.8.8;4.4.4.4;
addresses1=192.168.1.20;24;192.168.1.1;