| Red Hat This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-01-2005, 11:14 AM
|
#1
|
|
LQ Newbie
Registered: Aug 2003
Distribution: CentOS
Posts: 24
Rep:
|
How to "Search & Replace" in html files using Perl?
Hi,
I am a Perl newbie, so please be gentle with me.
I wish to insert <base href=http://www.ab.com> into a html file (say index.html) using a Perl script or Unix command. How do I do that?
Any advice much appreciated.
|
|
|
|
04-01-2005, 02:04 PM
|
#2
|
|
Member
Registered: Feb 2005
Location: Kansas City
Distribution: Kubuntu 8.04
Posts: 72
Rep:
|
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.
|
|
|
|
04-01-2005, 02:07 PM
|
#3
|
|
Member
Registered: Feb 2005
Location: Kansas City
Distribution: Kubuntu 8.04
Posts: 72
Rep:
|
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.
|
|
|
|
04-02-2005, 08:18 AM
|
#4
|
|
LQ Newbie
Registered: Aug 2003
Distribution: CentOS
Posts: 24
Original Poster
Rep:
|
Quote:
Originally posted by smannell
In Perl, you can open an existing file for reading, or appending; but to the best of my knowledge not for modifying.
|
Thanks very much for your response smannell. Very much appreciated.
I toyed with appending and then substitution in a Perl script but to no avail.
With appending, it always adds to the last line in the file. <base href=http://www.abc.com> in the last line is pretty useless.
Your comment that "appending can't modify" finally enlightened me that this is not the way to go.
With substitution, I don't know why but it just didn't work!
For example, I want to replace <head> with <head><base href=http://www.abc.com>
(which in effect adds the base href tag after the head tag)
s/<head>/<head><base href=http:\/\/www.abc.com>/g;
Nothing was changed at all.
|
|
|
|
04-02-2005, 08:25 AM
|
#5
|
|
LQ Newbie
Registered: Aug 2003
Distribution: CentOS
Posts: 24
Original Poster
Rep:
|
Quote:
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>/'
Last edited by rebel; 04-02-2005 at 08:27 AM.
|
|
|
|
04-02-2005, 08:26 AM
|
#6
|
|
LQ Newbie
Registered: Aug 2003
Distribution: CentOS
Posts: 24
Original Poster
Rep:
|
What finally did work is this Unix command:
> perl -pi -e 's/<head>/<head><base href=http:\/\/www.abc.com>/' index.html
Thanks again, smannell!
Last edited by rebel; 04-02-2005 at 08:34 AM.
|
|
|
|
04-06-2005, 05:17 PM
|
#7
|
|
Member
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459
Rep:
|
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 
Last edited by ivanatora; 04-06-2005 at 05:18 PM.
|
|
|
|
04-07-2005, 08:11 PM
|
#8
|
|
LQ Newbie
Registered: Aug 2003
Distribution: CentOS
Posts: 24
Original Poster
Rep:
|
Hi ivantora,
Thanks for your response.
Do we leave IN and OUT in the perl script, or replace them with the filenames? How do we run the perl script if we use IN and OUT?
|
|
|
|
04-09-2005, 12:58 PM
|
#9
|
|
Member
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459
Rep:
|
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";
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:14 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|