Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Logic Question

by wcj75019 (Acolyte)
on Feb 21, 2008 at 19:58 UTC ( [id://669374]=perlquestion: print w/replies, xml ) Need Help??

wcj75019 has asked for the wisdom of the Perl Monks concerning the following question:

I have a bunch of files. Some have the line change. Some do not and it needs to be added. Some have the the line, but it is wrong. So, I need to change it. I've tried it several different ways. Any thoughts would be appreciated. I would like each file to have:
'bgs') /sbin/sh /usr/adm/best1_default/bgs/scripts/best1agent_start >>\$LOG + 2>>\$LOG ;;
It is kind of a mess now. Thanks!
#!/usr/bin/perl -w use strict; my $CF_bgs = "watch process procAlive 'bgs' 8 0x2 60 'Productio +n Critical: bgs failure. Restarting bgs''/opt/EMPsysedge/bin/sysedge +.sh'"; my $bgs=<<HERE; 'bgs') /sbin/sh /usr/adm/best1_default/bgs/scripts/best1agent_start >>\$LOG + 2>>\$LOG ;; HERE #my $BestAgent = "/sbin/sh /usr/adm/best1_default/bgs/scripts/best1age +nt_start>>\$LOG 2>>\$LOG"; #my $BestCollect = "/usr/bin/su - patrol /usr/adm/best1_default/bgs/sc +ripts/best1collect -q>>\$LOG 2>>\$LOG"; open CF_FILES, "cf.files" or die "$!"; open SH_FILES, "sh.files" or die "$!"; my @cf_files=<CF_FILES>; my @sh_files=<SH_FILES>; close CF_FILES; close SH_FILES; my @CF_L = grep(/bgs/, @cf_files); foreach my $cf(@cf_files){ chomp $cf; my $cf_temp = "$cf".".sysedge"; open F, "$cf"; my @F_cf = <F>; # my @CF_L = grep("bgs", @F_cf); open(TMP, ">/tmp/$cf_temp"); my $line; while($line = <F>){ if($line =~ m#'bgs'#){ print $line; $line = "$CF_bgs\n"; #my $newline = $. + 1; print $line; } elsif ( $line =~ m#'syslogd'# ){ my $newline = $. + 1; $newline = $CF_bgs; print TMP "$newline\n"; } print TMP $line; } close TMP; close F; } #print @CF_L; #print @cf_files; #rename("out.$test", $test);

Replies are listed 'Best First'.
Re: Logic Question
by apl (Monsignor) on Feb 21, 2008 at 20:13 UTC
    Show us your code, tell us what's not working, and we'll be glad to take a look.

    This is another way of saying "Your question is not particularly specific".

Re: Logic Question
by toolic (Bishop) on Feb 21, 2008 at 20:12 UTC
    Could you elaborate on what you mean by "line change"?

    It may be helpful to post a few lines of the file you want to change, and how it should look after the change.

    Also, please post some of the code you have.

    By the way, you do not have to put the text of you question in "code" tags. But, you should put code in "code" tags.

Re: Logic Question
by hipowls (Curate) on Feb 21, 2008 at 20:14 UTC
Re: Logic Question
by ysth (Canon) on Feb 21, 2008 at 23:21 UTC
    Looks pretty good to me, at least to visual inspection, except that you try to read from F after you've already read the whole file into @F_cf (so there's nothing left to read). Replace your while loop with a for loop: for my $line (@F_cf) {

    Not sure what the final rename is; do you mean to rename your new file to the old name inside the outer for loop.

      Thank you. Very good point. yes, I am going to rename the file. But, see. I can't get the thing to work. I need to break it up into smaller chunks maybe.
        Get it working for one file, with a hardcoded filename. Then see if you are correctly generating a list of files. Then add in the loop over the list of files?
Re: Logic Question
by moklevat (Priest) on Feb 21, 2008 at 21:38 UTC
    I am guessing that when you write "line change" you mean newline. If that is the case, then you should take a look at this thread for several perl one-liners that might help. In particular I think you might find this node from tye helpful.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-23 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found