How to "Search & Replace" in html files using Perl?
Red HatThis forum is for the discussion of Red Hat Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
In Perl, you can open an existing file for reading, or appending; but to the best of my knowledge not for modifying. You'll have to make a new file, write what you want at the beginning, and then add the stuff from the old file. Finally you can rename the files. The nice part of doing it this way is you still have a copy of the original if you break things.
The basic steps are as follows (you'll have to modify the examples for your exact case):
Open a new file for writing
open (OUT, "> $outfile") || die "Cannot create file $outfile: $!";
where $outfile is a variable containing the filename, complete with path
Open the existing file you wish to modify
open (FILE, "$file") || die "Cannot open file $file: $!";
where $file is a variable containg the file name of the file you wish to read from
Write the line you want to the new file
print OUT "some text goes here\n";
Loop through the lines of the old file writing them to the new one
while ($line = <FILE>) {
print OUT "$line\n";
}
Close the file handles
close (FILE) || die "Can't close file $file\n";
close (OUT) || die "Can't close file $outfile\n"
Rename the files
rename ($file, "$file".".bak"); (syntax may be wrong here)
rename ($outfile, $file);
This may not be exactly what you need, and I haven't written a Perl script in a while, so there may be errors; but it will get you started. Also, there are examples of this on the web if you search for filehandles in Perl. Finally, a good perl book is essential if you plan on writting scripts.
I should read more carfully. If you want to use UNIX commands; "cat" will work for this. The man page is fairly straightforward. This will make for a much simpler script than using Perl.
Originally posted by smannell I should read more carfully. If you want to use UNIX commands; "cat" will work for this. The man page is fairly straightforward. This will make for a much simpler script than using Perl.
For some reason, the "cat and sed" Unix command didn't work.
> cat index.html | sed -e 's/<head>/<head><base href=http:\/\/www.miningnews.net>/'
I see the thread is finished, but here is the easiest (imo) way
Code:
open (IN,$file) or die "$!"; #<- open the file
@in = <IN>; #<- read it into an array
close(IN); #<- we don't need it anymore, so close it
foreach $line (@in){ #for each element of the array
$line =~ s|<head>|<head><base href=http://www.abv.com/|; #<- do the substitution, notice that you can use other letters except '/' for the s/// delimiters, in that case you don't need to escape the /'s later ;)
}
open (OUT,"> $file") or die "$!"; #<- open the file for writing
print OUT @in; #<- dump the modified content into it
close(OUT); #<- and close it
It's no difficult And you don't have to mix bash commands, and perl functions and other stuff. Hope I helped
IN and OUT are just filehandles. The thing you need is to set $file before the other lines (forgot to do that ). Like
$file = "/home/ivanatora/bleh/index.html";
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.