Editing multiple files
Posted by enquirer (enquirer), 16 December 2003I have a problem, simple, but, I'm a newbie, hence, finding it difficult:
I would like to change text, within multiple files.
replace text "audio/audio.html" inside a file
when a file is found in xx/yy.html (file called yy.html, in the directory
then replace text within it, from
"audio/audio.html" to "xx/yy.html"
If you can help, I'd be very grateful.
Posted by graham (graham), 16 December 2003A surprisingly common requirement in Perl - to go through all the files in a directory and replace all occurrences of one string in all files with a particular extension with another string. Here's a sample program that does it:
and the result from a sample run ....
Note that this example
a) does not decend into subdirectories
b) Uses full perl regular expressions for the string to be replaced
c) Takes a backup copy of each file being potentially changed
d) Gives a verbose report which could easily be shortened
e) Is designed to be fast on lots of small files; won't work on huge files that are too big to be loaded into memory in a single read
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho