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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: File handling
by friedo (Prior) on May 30, 2007 at 11:00 UTC
    perl -pi.bak -e 's/manulation/modification/' file.txt

    The -i.bak option will make a backup copy called file.txt.bak in case your regex goes disastrously wrong.

      perl -pi.bak -e 's/manulation/modification/' file.txt

      Actually that will change the first (and supposedly unique) occurrence of 'manulation' in every line: indeed this makes sense, because the OP's request is sloppy to begin with. Anyway, he stresses that the substitution he wants must take place in a "particalur line" and mentions Tie::File, although to the effect of not wanting to use it. Thus I suspect he really wants the substitution to happen on a given line. But then... editi: given by what? If by a line number, say the tenth, then it's easy; you just have to enhance the above to:

      perl -pi.bak -e '$.==10 and s/manulation/modification/' file.txt

      If it's something different, then you have to tell us.

        monks
        I have to use this in file manipulation like,
        open(FILE,"....") or die $!; while(<FILE>) { //Here Change part has to come } close FILE
        1)how to open a file like >+ or +<?.
        2)how to modify with in this file in while loop
        Thanks in advance
Re: File handling
by chrism01 (Friar) on May 31, 2007 at 07:14 UTC
    One way is to 'slurp' the file into an array (1 rec per element), then amend array, then re-write file.
    There's a good example here (http://www.goldb.org/goldblog/CommentView.aspx?guid=a858e606-de5d-4378-b538-8d4738cd9438) if I'm allowed to point to it ...

    Cheers

    Chris