| |||||||||||
| |||||||||||
Editing multiple files Posted by enquirer (enquirer), 16 December 2003 I have a problem, simple, but, I'm a newbie, hence, finding it difficult:I would like to change text, within multiple files. eg replace text "audio/audio.html" inside a file with ... directory name/filename.html eg when a file is found in xx/yy.html (file called yy.html, in the directory called xx) 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 2003 A 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:Code:
and the result from a sample run .... Code:
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 This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |