20ee Editing multiple files - Perl Programming
Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
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:
#!/usr/bin/perl

print "Directory path? ";
chop ($dp = <STDIN>);

print "File extension? ";
chop ($fe = <STDIN>);

print "Replace: ";
chop ($togo = <STDIN>);

print "By: ";
chop ($tobecome = <STDIN>);

opendir (DH,$dp) or die "Can't access directory\n";

while ($olditem = $item = readdir DH) {
       if ($item !~ /\.$fe$/) {
               print "Skipping $item\n";
               next;
               }
       $olditem =~ s/\.$fe$/.bak/;
       rename "$dp/$item","$dp/$olditem";
       open (FHI,"$dp/$olditem");
       open (FHO,">$dp/$item");
       read(FHI,$buffer,-s "$dp/$olditem");
       $count = ($buffer =~ s/$togo/$tobecome/g);
       print (($count+0)." changes to file $item\n");
       print FHO $buffer;
       }


and the result from a sample run ....

Code:
[Graham-Elliss-Computer:~/dec03] graham% perl multi_edit
Directory path? demo
File extension? html
Replace: and
By: &amp;
Skipping .
Skipping ..
Skipping bits.php4
Skipping ctown.php4
Skipping demopic.jpg
Skipping index.php4
Skipping page.bak
1 changes to file page.html
Skipping secdo.php4
Skipping sh.php4
Skipping sheep.jpg
Skipping T243.bak
56 changes to file T243.html
Skipping tab.bak
0 changes to file tab.html
Skipping upload.php4
Skipping WHC.css
[Graham-Elliss-Computer:~/dec03] graham%


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.
fb5b

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2013: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho
0