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

Hello Monks I have a file that I want to modify which conatains the follwing :
#MYFILE if [ $me = you ] then export ORA1=one export ORA2=3f34 export ORA3=zzzz fi
what I want to do is change the value of ORA3 to become 0; I am now copying to file and trying to modify it and rename it . Don't seem to do the right thing.
open MYFILE, "<$myfile"; open COPYFILE, ">$copyfile"; while(<MYFILE>){ if (m/(ORA3=)(\w+)/) { $value=$2 } $value =~ s/$value/0/; print COPYFILE $_; } close MYFILE; close COPYFILE; system (" mv COPYFILE MYFILE ");
Can someone help in finding the problem or advice on a better way. thanks

Replies are listed 'Best First'.
Re: manpulating a file
by Zaxo (Archbishop) on Jan 15, 2004 at 05:44 UTC

    A simplification of your code will fix it,

    while(<MYFILE>){ s/ORA3=\w+/ORA3=0/; print COPYFILE $_; }
    The space in your match was hurting you.

    And then there is always the command-line edit-in-place version: $ perl -pi -e's/ORA3=\w+/ORA3=0/' file

    After Compline,
    Zaxo

      Zaxo , thanks much for the quick respones . thats helped a lot.
Re: minpulating a file
by chimni (Pilgrim) on Jan 15, 2004 at 08:12 UTC

    While Zaxo's answer solves the problem.There is just on point i would like to make.
    I am a big fan of perl but sometimes perl may not be the most convinient way to do something.
    I feel this is one such scenario
    The above file is obviously from the "*nix" world.
    Since you want to modify the file, i would do so using vi rather than write a program.
    vi myfile
    press shift and : together
    %s/\(ORA3=\)\(.*\)/\10/g.
    :wq
    Of course the above can be done in many other ways.
    My basic point is why script when you can do this in 15 seconds in one line.
    HTH