The perlrun manpage has some switches which will let you do "in-place" file edits, something like this:
#!/usr/bin/perl -pi.orig
if(should_change($_)){
$_ = change_line($_);
}
| [reply] [d/l] |
You should be looking at the seek and
tell functions.
--
<http://www.dave.org.uk>
"Perl makes the fun jobs fun
and the boring jobs bearable" - me
| [reply] |
You will at least need to read the line that needs to be checked. This will probably require reading the file up to that point. However, it may be cheaper from a memory point of view to read the file to that point (not saving it), determine if the change is needed, then seek back to the beginning and process the file if the change is needed (no reason to close it in this case). Then you can:<bl>- Write the edited version to a temporary file
- Rename or unlink the original file
- Rename the temporary file with the original name
</bl>
I was going to suggest the -i switch until I thought about it a bit more and realized that you probably don't want to always write if the editing is not necessary (and I don't know about the seekability of the <> input, either). | [reply] |
I think the best answer to your question can be found here: perl -i
hope it helps!
-malloc | [reply] |
perl -pi -e "s/something_regexp/something_else/g;" <filenames>
at the command line will do the job
Using the above with perl -pi.bak .... will put the original file
into a backup copy called <orig>.bak | [reply] [d/l] [select] |