perl搜索并替换为新行
我需要能够运行一个perl脚本来搜索并替换[RemotePhoneBook0]下的3行 以下是该文件的摘录:perl搜索并替换为新行,perl,Perl,我需要能够运行一个perl脚本来搜索并替换[RemotePhoneBook0]下的3行 以下是该文件的摘录: [ RemotePhoneBook0 ] path = /config/Setting/Setting.cfg URL = Name = [ RemotePhoneBook1 ] path = /config/Setting/Setting.cfg URL = Name = 我不能触摸[RemotePhoneBook1]。完成后,上面的这段摘录应该是这样的: [ RemotePhon
[ RemotePhoneBook0 ]
path = /config/Setting/Setting.cfg
URL =
Name =
[ RemotePhoneBook1 ]
path = /config/Setting/Setting.cfg
URL =
Name =
我不能触摸[RemotePhoneBook1]。完成后,上面的这段摘录应该是这样的:
[ RemotePhoneBook0 ]
path = /somePath/to/someDir
URL = someUrl
Name = someName
[ RemotePhoneBook1 ]
path = /config/Setting/Setting.cfg
URL =
Name =
s/^<<<what can i put here>>>\s*=.*/somePath/;
s/^<<<what can i put here>>>\s*=.*/someUrl/;
s/^<<<what can i put here>>>\s*=.*/someName/;
[RemotePhoneBook0]
path=/somePath/to/someDir
URL=someUrl
Name=someName
[RemotePhoneBook1]
path=/config/Setting/Setting.cfg
网址=
名字=
s/^\s*=.*/somePath/;
s/^\s*=.*/someUrl/;
s/^\s*=.*/someName/;
对于这样的任务,我会使用Config::IniFiles
:
use warnings;
use strict;
use Config::IniFiles;
my $ini = Config::IniFiles->new( -file => "stackoverflow_30472923.ini" );
# print $ini->val("RemotePhoneBook0", "path");
$ini -> setval('RemotePhoneBook0', 'path', '/somePath/to/someDir');
$ini -> setval('RemotePhoneBook0', 'URL' , 'someUrl' );
$ini -> setval('RemotePhoneBook0', 'Name', 'someName' );
$ini -> WriteConfig("stackoverflow_30472923.modified.ini");
对子孙后代来说,这就是我所做的:
open my $in,"<","$directory/$files";
open my $out,">","$directory/temp.txt";
my @lines = <$in>;
close $in;
#print "@lines";
for($index=0;$index<=$#lines;$index++){
my $this = $lines[$index];
if ($this eq "[ RemotePhoneBook0 ]\n"){
$lines[$index] = "[ RemotePhoneBook0 ]\n";
$lines[$index+1] = "path=\n";
$lines[$index+2] = "URL = http:\/\/someUrl\n";
$lines[$index+3] = "Name = Users\n";
$lines[$index+4] = "\n";
$index++;
$index++;
$index++;
$index++;
}else{
$lines[$index] = $this,"\n";
}
}
print $out @lines;
在“,”中打开我的$directory/temp.txt“;
我的@lines=;
以美元收盘;
#打印“@行”;
对于($index=0;$index),您可以执行以下操作,匹配RemotePhoneBook0
,然后读取和编辑接下来的三行。它看起来像一个ini文件。请查看我正在使用的服务器的管理员,他们不允许我添加任何模块,并且此服务器缺少ini文件。。。。。