Tie::File represents a regular text file as a Perl array.
Each element in the array corresponds to a record in the
file. The first line of the file is element 0 of the
array; the second line is element 1, and so on.
The file is not loaded into memory, so this will work even for gigantic files.
Changes to the array are reflected in the file immediately.
Cheers,
Max | [reply] |
I recommend that you keep using your current approach (save as temp, delete original, and put temp in place of original). It's a very robust approach. It insures that you will always have a complete copy of the file, even if an error occurs during the save -- an error such as running out of disk space, etc. I would strongly caution against using any approach that destroys/overwrites/deletes the only copy of the file. | [reply] |
perl -i -pe 's/OLD/NEW/g' <file_name>
there are lot of unix utilities are there to do the same. ( sed, awk , so on )
hmmm ....let me think what did I said
| [reply] [d/l] |
echo ',s/foo/bar/g
wq'|ed somefile
Yes, that's a newline halfway through the command. | [reply] [d/l] |
perl -p -i.bak -e 's/ABC/DEF/' filename
#will save original as .bak
(: Life is short enjoy it :)
| [reply] [d/l] |
You could open the file for read and write, find the line of interest, seek backwards to start of line, then overwrite it. BUT, I agree with MonkE. Text files lines are usually variable length. This means that for the single file approach to work the new line must be exactly the same length as the original. | [reply] |
Imagine you're editing a site. Some email is spelled wrong so.. you gotta open up 40k lines of html code some stupid wysiwyg spat out. .. oh dear... well..
find ~/public_html -type f -name "*.htm" -exec perl -i -pe 's/wrongo\@hi\.com/righto\@yo\.net/sig' '{}' \;
I know of a case where we had a client come in, and they had hired someone full time for 6 months to fix things in hand made html that would have taken a posix monkey five seconds to do.
| [reply] [d/l] |