|Home » Faqs » File io » Edit in place|
I have some code that makes changes to the contents of a file and writes the changes to a temporary file. I then have to rename the temporary file back to the original file.
Is there a way to do this without ever needing to create a temporary file?
When you run perl with the -i flag it allows you to edit files in place, (and optionally to create a backup file).
perl -pi -e 's/abc/XYZ/' file.txt
will overwrite the original 'file.txt' file with the edited file.
If you provide an argument for the -i flag, it allows you to edit files in place, and make a backup file create a backup file).
perl -pi '.orig' -e 's/abc/XYZ/' file.txt
will make changes to file.txt and save the original file in file.txt.orig
See perldoc perlrun for more info.