in reply to if elsif elsif prints all three.

while ($line = <SH>){ if($line =~ m/$agent/){ $line = $collect; #this line will be replaced by the $collect line in the file OUT } elsif ( $line =~ m/best1collect/) { next; #this line will NOT appear in the file OUT, because you #don't reach the print command. If you want this line in #the OUT file, I suggest, you use a continue block. See at #the end. } elsif ($line =~ m/'syslogd'\)/){ my $newline = $. + 3; $newline = $bgs; print OUT "$newline\n"; # this line will appear in the OUT file , but have the # bgs line inserted before. } # all other lines will be copied. print OUT $line; print $line; }
Here is the version that does not omit lines. The statements in the continue block are executed always at the end of each loop.
while ($line = <SH>){ if($line =~ m/$agent/){ $line = $collect; } elsif ( $line =~ m/best1collect/) { next; } elsif ($line =~ m/'syslogd'\)/){ my $newline = $. + 3; $newline = $bgs; print OUT "$newline\n"; } } continue { print OUT $line; print $line; }

Replies are listed 'Best First'.
Re^2: if elsif elsif prints all three.
by wcj75019 (Acolyte) on Feb 27, 2008 at 23:20 UTC
    Ok, it doesn't remove that line and if the $line isn't there. It put the $ +bgs here doc there. But, if it has the wrong $line. if($line =~ m/$agent/){ $line = $collect; It fixes it and also adds the $bgs here. same with if the $line is right. It doesn't remove the line anymore. + But, it adds the $bgs here to that file too. Thanks!