----- Original Message ----- From: "loan tran" <[EMAIL PROTECTED]> To: "perl beginners" <[EMAIL PROTECTED]> Sent: Monday, October 11, 2004 3:36 PM Subject: Re: Can do in 1 while loop?
> > --- Ramprasad A Padmanabhan > <[EMAIL PROTECTED]> wrote: > > > On Mon, 2004-10-11 at 06:26, loan tran wrote: > > > Hello Perl Gurus, > > > > > > I wrote a script to search for log suspends and > > > bloking processes in a text file and send me email > > if > > > it find either of them. My codes below work but > > it's > > > not efficent. As you can see I open the file and > > go to > > > while loop twice. Can someone suggest a better > > way? > > > Thanks. > > > (attached is the text file i open to search.) > > > Below is my codes: > > > > > > #!/bin/perl -w > > > require "/home/sybase/tranl/pl/global.pl"; > > > > > > ## Search for blocking process > > > > > > open (FILE,"<$whodo_out") or die ("Cannot open > > file: > > > $!"); > > > while (my $line =<FILE>){ > > > chomp($line); > > > $line =~ s/^\s+//; > > > next if ($line =~ /^\D/); > > > my $blk = substr($line,40,3); > > > print " $blk \n"; > > > if ($blk != 0){ > > > print 'Alert! Blocking processes'; > > > system("/usr/bin/mailx -s 'Alert Blocking > > > Process' $receipients < $whodo_out"); > > > } > > > print "\n $suspend \n"; > > > #exit ; > > > }#end while > > > > > > close (FILE); > > > > > > # Search for LOG SUSPEND process > > > > > > open (FILE,"<$whodo_out") or die ("Cannot open > > file: > > > $!"); > > > while (my $line =<FILE>){ > > > chomp($line); > > > $line =~ s/^\s+//; > > > next if ($line =~ /^\D/); > > > my $log_suspend = substr($line,70,11); > > > print "$log_suspend \n"; > > > if ($log_suspend eq 'LOG SUSPEND'){ > > > print 'Alert! LOG SUSPEND processes'; > > > system("/usr/bin/mailx -s 'Alert LOG > > SUSPEND > > > Process' $receipients < $whodo_out"); > > > } > > > > > > }#end while > > > > > > close FILE; > > > > > > ## > > > > > > > > I am not able to get it ? > > Why cant you put This inside your first while loop > > > > > my $log_suspend = substr($line,70,11); > > print "$log_suspend \n"; > > if ($log_suspend eq 'LOG SUSPEND'){ > > print 'Alert! LOG SUSPEND processes'; > > system("/usr/bin/mailx -s 'Alert LOG SUSPEND > > Process' $receipients < $whodo_out"); > > } > > > > > > > > > > Probably after > > "print "\n $suspend \n";" > > > > > > Bye > > Ram > > I have tried your suggestion. Howerver, result i got > is only the first if run ( reciveice only blocking > email). > > In my first post I forget a "last" statement in each > if. (b/c I dont want to receive multible emails if > there are multily blocking or suspending procecess.) > > So here is the code again: > > You should post your beggin code as well, so you can clearify what modules you are using. > open (FILE,"<$whodo_out") or die ("Cannot open file: > $!"); > while (my $line =<FILE>){ > chomp($line); > $line =~ s/^\s+//; > next if ($line =~ /^\D/); You should change the above logic, match will occure with spaces and line terminators > my $blk = substr($line,40,3); You can do both of your string manipulation statments here, unless you are only showing partial code. > if ($blk != 0){ > print "Alert Blocking processes\n"; > system("/usr/bin/mailx -s 'Alert BLOCKING > Process' $receipients < $whodo_out"); > last; > } > > }#end while > > close (FILE); > > > # Search for LOG SUSPEND process > > open (FILE,"<$whodo_out") or die ("Cannot open file: > $!"); > while (my $line =<FILE>){ > chomp($line); > $line =~ s/^\s+//; > next if ($line =~ /^\D/); > my $log_suspend = substr($line,70,11); > if ($log_suspend eq 'LOG SUSPEND'){ > print "Alert! LOG SUSPEND processes\n"; > system("/usr/bin/mailx -s 'Alert LOG SUSPEND > Process' $receipients < $whodo_out"); > last; > } > }#end while > > close FILE; Here is one way you can try it.#Untested #!PERL use warnings; use strict; my $whodo_out = $ARGV[0] || die "NO FILE TO PARSE\a\n"; my $receipients = $ARGV[1] || die "NO RECEIPIENTS ADDRESS\a\n"; open FILE,$whodo_out || die "Cannot open file: $!\n"; while (my $line =<FILE>){ next if ($line =~ /^[\s\D]+); chomp $line; # not sure if you need this chomp at all $line =~ s/^\s+//; my $blk = substr($line,40,3); my $log_suspend = substr($line,70,11); if ($blk != 0){ print "Alert Blocking processes\n"; system "/usr/bin/mailx -s 'Alert BLOCKING Process' $receipients < $whodo_out"; last; } if ($log_suspend eq 'LOG SUSPEND'){ print "Alert! LOG SUSPEND processes\n"; system "/usr/bin/mailx -s 'Alert LOG SUSPEND Process' $receipients < $whodo_out"; last; } }#end while close FILE; HTH, Mark G. > > > > > > > > > -- > > To unsubscribe, e-mail: > > [EMAIL PROTECTED] > > For additional commands, e-mail: > > [EMAIL PROTECTED] > > <http://learn.perl.org/> > > <http://learn.perl.org/first-response> > > > > > > > > > > > __________________________________ > Do you Yahoo!? > Yahoo! Mail Address AutoComplete - You start. We finish. > http://promotions.yahoo.com/new_mail > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > <http://learn.perl.org/> <http://learn.perl.org/first-response> > > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>