LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl script to replace a line after finding a previous line (https://www.linuxquestions.org/questions/programming-9/perl-script-to-replace-a-line-after-finding-a-previous-line-865408/)

Mark1986 02-28-2011 01:32 AM

Perl script to replace a line after finding a previous line
 
Hi all,

At the moment I have a flat file which is being used by a few people. I want a script to remotely change the file, so I can start logging who is doing what.

At this point here is one requirement I am trying to develop. We have text blocks who pretty much look like this:


---------------------- C001

$$var1=Source
$$var2=Target
$$var3=2



---------------------- C006

$$var1=Source
$$var2=Target
$$var3=3

In this text block I first want to find C001 and afterwards change $var3. Thing is, I don't want to change all $var3's.

This is my code so far;

Code:

        open(PARAM, "<be_cycle3b.prm") or die ("Kan parameter bestand niet openen.");

        my @dat_param = <PARAM>;

        foreach my $regel (@dat_param){

                if( $regel =~ /^-+ +$workflow/g ){

                        print $regel;

                        foreach (my $regel_n = @dat_param) {

                                print " \n regel_n: " , $regel_n  , " \n ";

                                if( $regel_n =~ /^$param/ ){
                                       
                                        print " foreach regel_n: ",$param;

                                        print $regel_n;
                                }
                        }


                }

        }

        close(PARAM);

I have not come to the point of changing $$var3. Even trying to read the line with $var3 is not possible yet.

Here are the used variables:

$workflow (C001 as in example given)

$param = $$var3

I hope this is somewhat clear. I try to find $param for the right $workflow and change that. Can you help me to find $$var3 and change that?

Much appreciated.

kris_kiil 02-28-2011 05:09 PM

There is more than one way to do it
 
My recommendation is to use a state structure, since this makes it fairly simple to see what is going on.

Code:

        open(PARAM, "<be_cycle3b.prm") or die ("Kan parameter bestand niet openen.");
       
        my @dat_param = <PARAM>;
        my $state = 0;
        foreach my $regel (@dat_param){
                if( $regel =~ m/^-+/ ){
                        if($regel =~ m/$workflow/){
                                #You found the right section
                                $state = 1;
                        }
                        else{
                                #You are in a wrong section
                                $state = 0;
                        }
                }
                if($state == 1 and $regel =~ m/^$param/){
                        # You have now read the right line,
                        # so do something useful below.
                        print "$regel";
                }
        }

This way you don't end up processing the wrong lines.

As for how to change $var3, just remember that, if $var = 'a' you can access $a as $$var, but read the perlreftut and/or perlref manpages for the complete story.

Good luck


All times are GMT -5. The time now is 12:11 PM.