Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

if elsif elsif prints all three.

by wcj75019 (Acolyte)
on Feb 27, 2008 at 20:28 UTC ( #670737=perlquestion: print w/ replies, xml ) Need Help??
wcj75019 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am changing/adding lines in bunch of files. if $line is something. Change it to something else. elsif $line is correct. leave it alone with next. elsif $line isn't there. Put, it there. If works, elsif removes the $line if the $line is there. elsif the line isn't there. Add it. That works. But,the if/elsif/ also add there stuff too. Then if I put else {next}; That Totally screws up the out file. Can you tell me what is wrong? Thanks!
#!/usr/bin/perl -w use strict; my $agent = "best1agent_start"; my $bgs=<<HERE; 'bgs') /usr/bin/su - patrol /usr/adm/best1_default/bgs/scripts/best1collect + -q>>\$LOG 2>>\$LOG ;; HERE my $collect = " /usr/bin/su - patrol /usr/adm/best1_default/bgs/scr +ipts/best1collect -q>>\$LOG 2>>\$LOG\n"; my $cf_dir = "/export/home/f358118/sysedge"; open SH_FILES, "sys.sh" or die "Error: $!"; my @sh_f=<SH_FILES>; close SH_FILES; foreach my $sh(@sh_f){ chomp $sh; open(OUT, ">/$cf_dir/$sh".".OUT")or die "ERROR $!"; my $line; open (SH, "$cf_dir/$sh") or die "ERROR $!"; 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"; } #else { next }; print OUT $line; print $line; } }

Comment on if elsif elsif prints all three.
Download Code
Re: if elsif elsif prints all three.
by pc88mxer (Vicar) on Feb 27, 2008 at 20:56 UTC

    } elsif ( $line =~ m/best1collect/) { next;

    This will omit lines containing 'best1collect' from the input file. Instead I think what you want is to set a flag so that later when you see 'syslogd' you do not insert the bgs stuff. Is that what you want to do?

      Yes if =~ m/bestcollect/ That means the line is ok. So, leave it alone. if there is no $line. Then insert the here documnet $bgs before 'syslogd' Thanks
      I want it to do nothing if the $line has /bestcollect/ It prints every if elseif. I want it to just do one thing. If the line is wrong. fix it. If it isn't there print the $bgs stuff. Thanks,
Re: if elsif elsif prints all three.
by chromatic (Archbishop) on Feb 27, 2008 at 21:05 UTC
    Then if I put else {next}; That Totally screws up the out file.

    Yes, it immediately skips the rest of the current loop body and moves to the next iteration. That means nothing will print for the current iteration. If that's not what you want, remove the next.

      That is what I want. I want it to leave the file alone. But, it removes the line. leaving bgs) ;; on the next line. then adds the $bgs here document also.
Re: if elsif elsif prints all three.
by Errto (Vicar) on Feb 27, 2008 at 22:06 UTC

    I think the idea of "leave it alone" is where you get confused. next means skip the rest of the loop, so the line containing best1collect will not be printed to OUT. Without referring to "leaving it alone," ask yourself these questions: 1) should the lines containing "best1collect" be printed? If yes, then you don't want to call next in that first elsif. 2) Should the lines containing "syslogd" be printed? If no, then you probably do want to call next after printing $newline.

    Remember, you're reading from one file and writing to another. Any lines that you skip will not be written to that second file. That's why "leave it alone" is the wrong way to think about it.

Re: if elsif elsif prints all three.
by hexcoder (Hermit) on Feb 27, 2008 at 22:26 UTC
    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; }
      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!
Re: if elsif elsif prints all three.
by pc88mxer (Vicar) on Feb 28, 2008 at 14:33 UTC
    Here's a crazy idea: put each little block of code in its own file. So, create a file with this content:

    #!/bin/sh /usr/bin/su - patrol /usr/adm/best1_default/bgs/scripts/best1collect - +q >> $1 2>&1

    and name it 'start-bgs' in some directory. Then do the same for all other other case blocks and name them similarly. Then your script just becomes:

    #!/bin/sh ... # set $agent to the agent you want to start cmdpath=$cmd_dir/start-$agent if ! -x $cmdpath; then echo Error - start script $cmdpath not found exit 1 fi $cmdpath $LOG # pass any relevant parameters here

    Then when you need to add/change/delete agents you are just replacing files instead of trying to edit a few lines in a file. This should make things a lot easier to manage, especially if you have several hundred files to update.

    It's a crazy idea!

      That makes sense. But, all the files are different. I can only change or add those lines to them. Thanks,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://670737]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-12-26 21:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls