解析文件perl [英] Parsing the file perl
问题描述
该程序将$ rf_id,$ date,$ qf_id和$ failure_msg准确地打印到文件中,问题是在获取第一个值时将$ rf_id的值存储为0,而其余的则根据需要打印相同的值rf_id是sumhow,仅在其他行的第一行中将其设置为0很好.
This programme acually prints the $rf_id,$date,$qf_id and $failure_msg into a file, the problem is while getting the first value it stores the value of $rf_id as 0 and rest is the same value is printed as desired.The rf_id is sumhow is getting set to 0 in only firstline in other lines its just fine.
#!/usr/bin/perl
use strict;
use warnings;
my $mailqdir = "/mail1.txt";
my $mqueue_directory = "/var/spool/mqueue/";
my $messages_removed = 0;
my $rf_id;
my $date;
my $temp;
my $tmp;
my @write_array;
my $to;
my $from;
my $subject;
my $path_to_sendmail = "/usr/sbin/sendmail -f";
my $mailsubject = "Subject: Deleted Queued Mails from LSMGR sendmail \n\n\n";
$ENV{"PATH"}="/usr/sbin/:/usr/sbin";
my $currentfile = `sendmail -bp`;
my $qf_id = 0;
my $failure_msg;
my $qf_file;
my $df_file;
open (MYFILE, ">/queue.txt");
print MYFILE "$currentfile";
close (MYFILE);
system("/etc/init.d/sendmail stop");
open(MYFILE,$mailqdir);
while (<MYFILE>){
if(/(?<=<)[^>]+(?=>\s*$)/g){
$temp=$_;
$temp =~ s/^\s+|s+$//g;
$rf_id = "$temp";
my $tmp = "$temp";
}
if (/(\w{14})/){
$qf_id=$1;
$qf_file = 'qf' . $1;
$df_file = 'df' . $1;
$date = substr($_,24,17);
next unless /(\w{14})/;
$temp = scalar <MYFILE>;
$temp =~ s/^\s+|s+$//g;
$failure_msg=$temp;
}
push(@write_array,"$qf_file:$failure_msg:$date:$rf_id");
$ENV{"PATH"}="/var/run/:/etc/rc.d/init.d/functions:/var/lock/subsys/:/etc/init.d/sendmail:/etc/init.d:/bin/:/usr/local/bin/:/var/spool/mqueue";
print "Removing $qf_file... \n";
print "Removing $df_file...\n";
system ("rm $mqueue_directory$qf_file");
system ("rm $mqueue_directory$df_file");
$messages_removed++;
}
close (MYFILE);
open (CODE,">/mail.txt");
print CODE "@write_array";
close (CODE);
system("/etc/init.d/sendmail start");
$to='';
$from='';
$ENV{"PATH"}="/usr/sbin/:/usr/sbin";
#system ("$path_to_sendmail $from $to < /mail.txt");
print "\n$messages_removed total \"Deferred Mails\" message(s) removed from ";
print "mail queue.\n";
这是我想解析的一种记录,这是示例之一:
This is a type of record i want parse this is one of the example:
q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
q2VDWKkY010408 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
q2VDWKkY010409 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
q2VDWKkY010410 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
推荐答案
我无法重现您的问题.经过简化和修复(无需环顾四周,在替换s
之前缺少反斜杠),您的代码即可
I cannot reproduce your problem. After simplifying and fixing (no lookaround needed, missing backslash before s
in substitution) your code to
#!/usr/bin/perl
use strict;
use warnings;
my $temp;
my $rf_id;
my $failure_msg;
my $messages_removed;
my @write_array;
while (<DATA>){
chomp;
if(/<[^>]+>\s*$/g){
$temp = $_;
$temp =~ s/^\s+|\s+$//g;
$rf_id = $temp;
}
my ($qf_file, $df_file, $date);
if (/^(\w{14})/){
$qf_file = 'qf' . $1;
$df_file = 'df' . $1;
$date = substr $_, 24, 17;
next unless /(\w{14})/;
$temp = scalar <DATA>;
$temp =~ s/^\s+|s+$//g;
$failure_msg = $temp;
}
push(@write_array,"$qf_file:$failure_msg:$date:$rf_id\n");
print "rm $qf_file $df_file\n";
$messages_removed++;
}
print "\n\nCODE\n@write_array\n";
print "\n$messages_removed total \"Deferred Mails\" message(s) removed from ";
print "mail queue.\n";
__DATA__
q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
q2VDWKkY010408 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
q2VDWKkY010409 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
q2VDWKkY010410 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
我得到以下输出:
rm qfq2VDWKkY010407 dfq2VDWKkY010407
Use of uninitialized value $qf_file in concatenation (.) or string at 1.pl line 29, <DATA> line 3.
Use of uninitialized value $date in concatenation (.) or string at 1.pl line 29, <DATA> line 3.
Use of uninitialized value $qf_file in concatenation (.) or string at 1.pl line 30, <DATA> line 3.
Use of uninitialized value $df_file in concatenation (.) or string at 1.pl line 30, <DATA> line 3.
rm
rm qfq2VDWKkY010408 dfq2VDWKkY010408
Use of uninitialized value $qf_file in concatenation (.) or string at 1.pl line 29, <DATA> line 6.
Use of uninitialized value $date in concatenation (.) or string at 1.pl line 29, <DATA> line 6.
Use of uninitialized value $qf_file in concatenation (.) or string at 1.pl line 30, <DATA> line 6.
Use of uninitialized value $df_file in concatenation (.) or string at 1.pl line 30, <DATA> line 6.
rm
rm qfq2VDWKkY010409 dfq2VDWKkY010409
Use of uninitialized value $qf_file in concatenation (.) or string at 1.pl line 29, <DATA> line 9.
Use of uninitialized value $date in concatenation (.) or string at 1.pl line 29, <DATA> line 9.
Use of uninitialized value $qf_file in concatenation (.) or string at 1.pl line 30, <DATA> line 9.
Use of uninitialized value $df_file in concatenation (.) or string at 1.pl line 30, <DATA> line 9.
rm
rm qfq2VDWKkY010410 dfq2VDWKkY010410
Use of uninitialized value $qf_file in concatenation (.) or string at 1.pl line 29, <DATA> line 12.
Use of uninitialized value $date in concatenation (.) or string at 1.pl line 29, <DATA> line 12.
Use of uninitialized value $qf_file in concatenation (.) or string at 1.pl line 30, <DATA> line 12.
Use of uninitialized value $df_file in concatenation (.) or string at 1.pl line 30, <DATA> line 12.
rm
CODE
qfq2VDWKkY010407:(host map: lookup (my.local.domain): deferred)
:Sat Mar 31 19:37 :q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>
:(host map: lookup (my.local.domain): deferred)
::<yagyavalkbhatt@yahoo.com>
qfq2VDWKkY010408:(host map: lookup (my.local.domain): deferred)
:Sat Mar 31 19:37 :q2VDWKkY010408 2221878 Sat Mar 31 19:37 <Mailer-daemon>
:(host map: lookup (my.local.domain): deferred)
::<yagyavalkbhatt@yahoo.com>
qfq2VDWKkY010409:(host map: lookup (my.local.domain): deferred)
:Sat Mar 31 19:37 :q2VDWKkY010409 2221878 Sat Mar 31 19:37 <Mailer-daemon>
:(host map: lookup (my.local.domain): deferred)
::<yagyavalkbhatt@yahoo.com>
qfq2VDWKkY010410:(host map: lookup (my.local.domain): deferred)
:Sat Mar 31 19:37 :q2VDWKkY010410 2221878 Sat Mar 31 19:37 <Mailer-daemon>
:(host map: lookup (my.local.domain): deferred)
::<yagyavalkbhatt@yahoo.com>
8 total "Deferred Mails" message(s) removed from mail queue.
请尝试解释您要实现的目标.在运行代码时,请注意您收到的警告-您正在使用空文件名运行rm
(可以用Perl的unlink
代替).
Please, try to explain what you are trying to achieve. When running code, please pay attention to the warnings you are getting - you are running rm
(which could be replaced by Perl's unlink
) on an empty filename.
更新:疯狂猜测您可能要做什么:
Update: Wildly guessing what you might be up to:
#!/usr/bin/perl
use strict;
use warnings;
my %matched;
my @out;
while (<DATA>) {
chomp;
if (/^(?<id>\S{14})\s+
(?<num>[0-9]+)\s+
(?<day>...)\s+
(?<month>...)\s+
(?<monthday>[0-9]+)\s+
(?<time>[0-9]+:[0-9]+)\s+
<(?<from>.*)>/x) {
%matched = %+;
} elsif (/^\s+<(.*)>/) {
$out[-1] .= ":$1";
} elsif (s/^\s+\(/(/) {
print "rm qf$matched{id} df$matched{id}\n";
push @out, join ':', "qf$matched{id}",
$_,
join ' ',@matched{qw/day month monthday time/};
} else {
die "Cannot parse: $_\n";
}
}
print "$_\n" for @out;
print scalar @out, " messages removed.\n";
__DATA__
q2VDWKkY010407 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
q2VDWKkY010408 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
q2VDWKkY010409 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
q2VDWKkY010410 2221878 Sat Mar 31 19:37 <Mailer-daemon>
(host map: lookup (my.local.domain): deferred)
<yagyavalkbhatt@yahoo.com>
请注意,:
作为定界符可能不是最佳选择:它在时间上和消息中两次使用.
Note that :
as a delimiter is probably not the best choice: it is used in time and twice in the message, too.
这篇关于解析文件perl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!