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>
I cannot reproduce your problem. After simplifying and fixing (no lookaround needed, missing backslash before
s
in substitution) your code toI am getting this output:
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'sunlink
) on an empty filename.Update: Wildly guessing what you might be up to:
Note that
:
as a delimiter is probably not the best choice: it is used in time and twice in the message, too.