我有以下数据:
源文件:input.txt
包含许多具有以下格式的记录:
09.05.2014 09.49.52:359 RID:routerNode1@app1:1662306081,msisdn:787887225696,sid:93889095007001,tid:1405090902095648846024000,status:2,time:20140509094952,reasonELIVRD,refund:null,status2:null
output1.txt
:
09.05.2014,09.49.52,routerNode1@app1:1662306081,787887225696,93889095007001,1405090902095648846024000,2,20140509094952
output2.txt
09-05-2014,09-49-52,routerNode1@app1:1662306081,787887225696,93889095007001,1405090902095648846024000,2,20140509094952
请帮我构建perl程序,从输入文件中读取,然后写入输出文件。
此代码从行读取,尚未从输入文件中读取。但还是有问题
给出的数据不是truedata给出的不是truedata给出的不是truedata给出的不是真的,787887225696,93889095007001,1405090902095648846024000,2,20140509094952
#!/usr/bin/perl
use warnings;
use strict;
my $str = <<'_STR_';
09.05.2014 09.49.52:359 RID:routerNode1@app1:1662306081,msisdn:787887225696,sid:93889095007001,tid:1405090902095648846024000,status:2,time:20140509094952,reason:DELIVRD,refund:null,status2:null
_STR_
my $wanted_data;
for ( split /,/, $str ) {
if (/(\d{2}\.\d{2}\.\d{4}' '\d{2}\.\d{2}\.\d{2}).+RID:(\d+)/sm) {
my ( $format_date, $rid )= ( $1, $2 );
$format_date =~ s/(\d{4})(\d{2})(\d{2})/$1-$2-$3/;
$wanted_data .= $format_date . ',' . $rid;
}
elsif (/msisdn:(.+)/) {
$wanted_data .= ',' . $1;
}
elsif (/sid:(.+)/) {
$wanted_data .= ',' . $1;
}
elsif (/tid:(.+)/) {
$wanted_data .= ',' . $1;
}
elsif (/status:(.+)/) {
$wanted_data .= ',' . $1;
}
elsif (/time:(.+)/) {
$wanted_data .= ',' . $1;
}
else{ print "data given is not true"}
}
print $wanted_data, $/;
答案 0 :(得分:0)
您可以尝试以下perl脚本:
#! /usr/bin/perl
use warnings;
use strict;
while (<>) {
my @out;
my @fld=split(/,/);
my ($format_date, $rid)=$fld[0]=~/(\d{2}\.\d{2}\.\d{4} \d{2}\.\d{2}\.\d{2}).+RID:(.*)$/;
badInput() if ! defined $format_date;
my @f=split(" ",$format_date);
push(@out,$f[0]=~s/\./-/gr);
push(@out,$f[1]=~s/\./-/gr);
push(@out,$rid);
my $i=1;
for (qw(msisdn sid tid status time)) {
my ($id)=$fld[$i++]=~/$_:(.*)$/;
badInput() if ! defined $id;
push(@out,$id);
}
print join(",",@out)."\n";
}
sub badInput {
die "Data given is not true!";
}