in reply to Substitution of text within a file

There's really no need for a program here. You can do it all from the command line.

$ perl -i.bak -pe 's/(\d+)$/+615$1/ if /^0001/' your_filename
--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re: Re: Substitue!
by bart (Canon) on Jun 07, 2003 at 06:17 UTC
    Indeed. All you need is the -i option, and possibly the -p option (see perlrun) to do the loop and the printing. You can even use them in regular scripts. Note that with the -p option, anything you want to do before the loop needs to be in a BEGIN block.

    In the next code, the -l option takes care of the newlines.

    #!/usr/bin/perl -p -i.bak -l BEGIN { @ARGV = 'personel.txt'; $newdigit="+615"; $id="0001"; } if(/^$id\|/o) { my($id, $name, $family, $oldphone) = split /\|/; $newphone = $newdigit .$oldphone; $_ = join ("|", $id, $name, $family, $newphone); }

    p.s. You're trying to read from a file opened for append. Jeez.

      Bart, tanx for ur reply. Your code is working perfectly, however if i try to call it from a web browser through a get method (passing the id) it won't work!.. any clue why this is happening..