in reply to Re: Searching and replacing text
in thread Searching and replacing text

But you can't just substitute arbitrary text in a text file, unless it happens to have exactly the same length. As file_b is being read and searched, each line should be copied out to a new file, and updated line printed in it's proper order, and the remainder of file_b copied. Then rename the new file to file_b (making sure file_b is closed).

Replies are listed 'Best First'.
Re^3: Searching and replacing text
by graff (Chancellor) on Jul 29, 2004 at 06:15 UTC
    Yes, I glossed over the output management part, thinking the OP probably had a handle on that already (unfortunate pun duly noted).

    As often as not for things like this, I just make the script print to STDOUT and put the file handling stuff on the command line -- why right write more lines of perl code to do stuff that the shell can do for me with an angle bracket?

    text-altering-script.perl input.file > output.file # optional step: mv output.file input.file