Perl 在多个文件中搜索一个模式,并用增量替换现有匹配
我有大约200个文件,所有的文件都有相同的值。现在想搜索一个模式,然后 替换匹配字符 例如:Perl 在多个文件中搜索一个模式,并用增量替换现有匹配,perl,Perl,我有大约200个文件,所有的文件都有相同的值。现在想搜索一个模式,然后 替换匹配字符 例如: file01.txt: 1,51:495600 118,1:20140924105028 file02.txt have 1,51:495600 118,1:20140924105028 1,51:495601 /* each file ':number' will be incremented by one 118,1:20140924105228
file01.txt
:
1,51:495600
118,1:20140924105028
file02.txt have
1,51:495600
118,1:20140924105028
1,51:495601 /* each file ':number' will be incremented by one
118,1:20140924105228 /* each file 11th and 12th position character will be incremented by 02
1,51:495602 /* each file ':number' will be incremented by one
118,1:20140924105428 /* each file 11th and 12th position character will be incremented by 02
for earlier it was 50 now it will incremented by 52 , 53 , 54 like that max will be 60
e.g..
file01.txt
118,1:20140924105028 ;
file02.txt
118,1:20140924105228 ;
file03.txt
118,1:20140924105428 ;
file04.txt
118,1:20140924105628 ;
file05.txt
118,1:20140924105828 ;
。。。依此类推,直到file200.txt
我们的结果应该是:
file01.txt
:
1,51:495600
118,1:20140924105028
file02.txt have
1,51:495600
118,1:20140924105028
1,51:495601 /* each file ':number' will be incremented by one
118,1:20140924105228 /* each file 11th and 12th position character will be incremented by 02
1,51:495602 /* each file ':number' will be incremented by one
118,1:20140924105428 /* each file 11th and 12th position character will be incremented by 02
for earlier it was 50 now it will incremented by 52 , 53 , 54 like that max will be 60
e.g..
file01.txt
118,1:20140924105028 ;
file02.txt
118,1:20140924105228 ;
file03.txt
118,1:20140924105428 ;
file04.txt
118,1:20140924105628 ;
file05.txt
118,1:20140924105828 ;
file02.txt
:
1,51:495600
118,1:20140924105028
file02.txt have
1,51:495600
118,1:20140924105028
1,51:495601 /* each file ':number' will be incremented by one
118,1:20140924105228 /* each file 11th and 12th position character will be incremented by 02
1,51:495602 /* each file ':number' will be incremented by one
118,1:20140924105428 /* each file 11th and 12th position character will be incremented by 02
for earlier it was 50 now it will incremented by 52 , 53 , 54 like that max will be 60
e.g..
file01.txt
118,1:20140924105028 ;
file02.txt
118,1:20140924105228 ;
file03.txt
118,1:20140924105428 ;
file04.txt
118,1:20140924105628 ;
file05.txt
118,1:20140924105828 ;
因为我是Perl新手,所以我需要帮助来创建脚本
找到下面的脚本
foreach $file (@files)
{
open(file , "$file"); #open file
while($line = <file>) #read file
{
print "$line" if $line =~ /1,51:495600/; #find patten
perl -pi'.backup' -e 's/(^1,51:495600)/^1,51:/g' $file
#find and replace and take a backup of file
}
close;
}
foreach$文件(@files)
{
打开(文件,“$file”)35;打开文件
而($line=)#读取文件
{
如果$line=~/1,51:495600/;#查找模式,则打印“$line”
perl-pi'.backup'-e's/(^1,51:495600)/^1,51:/g'$file
#查找、替换并备份文件
}
接近;
}
这将按您的要求执行。它使用模块操作日期/时间字段,以便正确处理小时界限和午夜。这是Perl5版本10以来的核心模块,因此不需要安装
use strict;
use warnings;
use 5.010; # For autodie and regex \K
use autodie;
use Time::Piece;
use Time::Seconds qw/ ONE_MINUTE /;
use constant DATE_FORMAT => '%Y%m%d%H%M%S';
my $n;
for my $file (@files) {
open my $in_fh, '<', $file;
my @lines = <$in_fh>;
close $in_fh;
++$n;
$lines[0] =~ s/^\d+,\d+:\K(\d+)/$1+$n/e;
$lines[1] =~ s{^\d+,\d+:\K(\d+)}{
my $tp = Time::Piece->strptime($1, DATE_FORMAT);
($tp + ONE_MINUTE * 2 * $n)->strftime(DATE_FORMAT);
}e;
my $backup = "$file.backup";
unlink $backup if -f $backup;
rename $file, $backup;
open my $out_fh, '>', $file;
print $out_fh @lines;
close $out_fh;
}
使用严格;
使用警告;
使用5.010;#用于autodie和regex\K
使用自动模具;
使用时间::件;
使用时间:秒qw/一分钟/;
使用固定日期格式=>'%Y%m%d%H%m%S';
我的$n;
对于我的$file(@files){
在“,”中打开我的$文件;
打印$out_fh@行;
关闭$OFH;
}
请解释第二行的行为。字符11和12是50
。听起来你想在第一个文件中添加2个,在第二个文件中添加3个,等等。但是你的示例在第一个文件中添加2个,在第二个文件中添加4个,在第三个文件中添加6个,等等。你所说的“max将是60”是什么意思?在file09.txt
(或file05.txt
)中该字段达到60
后会发生什么情况取决于算法)。118,1:20140924105028如果我们将字段“20140924105028”打断,可以理解如下所述:2014 09 24 10 50 28 yyyy mm dd hh24 mi ss现在的想法是增加分钟(在上述情况下为50)增加2分钟,直到达到60。一旦达到60,小时列应额外增加1小时(在本例中为11)分钟计数器应该以00,02,04,…60开头。例如:-118,1:20140924105228 118,1:20140924105428 118,1:20140924105628 118,1:20140924105828 118,1:20140924110028 118,1:2014092411028 118等等。使用perl,v5.10.1
我得到全局符号“@files”在…第13行需要明确的包名。