Replacing a text in multiple files with regular expressions

A

Angel

I would like to make an image clickable, in other words
to replace all occurrences of:

<img src="pic.gif" width="199" height="31" alt="" border="0">

with

<a href="http://somewhere.com"><img src="pic.gif" width="199"
height="31" alt="" border="0"></a>

I would like to search and replace on many files located in
different subfolders of one main folder.

I know it should be possible using regular expressions but
all my attempts failed. Can anyone help me ?

Thanks.
 
M

Matt Garrish

Angel said:
I would like to make an image clickable, in other words
to replace all occurrences of:

<img src="pic.gif" width="199" height="31" alt="" border="0">

with

<a href="http://somewhere.com"><img src="pic.gif" width="199"
height="31" alt="" border="0"></a>

I would like to search and replace on many files located in
different subfolders of one main folder.

I know it should be possible using regular expressions but
all my attempts failed. Can anyone help me ?

If you want to make *all* the images clickable (and none of them already
are), you could try something like:

$html =~ s#(<img[^>]+>)#<a href="http://somewhere.com">$1</a>#gis;

That said, regexes are not the best way to deal with markup languages, as
you no doubt are finding out. You're better off taking a look at one of the
html parsing modules.

Matt
 
A

Angel

I would like to make an image clickable, in other words
to replace all occurrences of:

<img src="pic.gif" width="199" height="31" alt="" border="0">

with

<a href="http://somewhere.com"><img src="pic.gif" width="199"
height="31" alt="" border="0"></a>

I would like to search and replace on many files located in
different subfolders of one main folder.

I know it should be possible using regular expressions but
all my attempts failed. Can anyone help me ?

Thanks.

The problem was not the regular expression itself but
the need to process many files and recursively to search
the sub-folders.

I tried something similar to:

perl -pi.bak -e "s#(<img[^>]+>)#<a href="http://somewhere.com">$1</a>#gi" *.html

The problem is that wildcards does not work with my
perl on Windows XP. I am not sure if it works in Unix either.

Luckily I found a perl script that does exactly what I need.
You can find it here:

http://peter.verhas.com/progs/perl/prep/

Thanks Verhás Péter :)

The link to download the script is at the end of the page.
 
J

Jürgen Exner

Angel said:
(e-mail address removed) (Angel) wrote in message


The problem was not the regular expression itself but
the need to process many files and recursively to search
the sub-folders.

Then why didn't you say so in the beginning?
And btw., no it is not possible to recurse through a directory structure
using REs. Those two have nothing to do with each other.
I tried something similar to:

perl -pi.bak -e "s#(<img[^>]+>)#<a
href="http://somewhere.com">$1</a>#gi" *.html

The problem is that wildcards does not work with my
perl on Windows XP. I am not sure if it works in Unix either.

The proper way is to use File::Find
It will recurse through any directory structure, visiting any file on the
way.
Then it is up to you to do whatever you want to for each file.

jue
 
T

Tad McClellan

Angel said:
The problem is that wildcards does not work with my
perl on Windows XP.


But you can make "wildcards" work yourself by using:

perldoc -f glob
 
A

Angel

Jürgen Exner said:
Then why didn't you say so in the beginning?
And btw., no it is not possible to recurse through a directory structure
using REs. Those two have nothing to do with each other.

I said that:

"I would like to search and replace on many files located in
different subfolders of one main folder."
I tried something similar to:

perl -pi.bak -e "s#(<img[^>]+>)#<a
href="http://somewhere.com">$1</a>#gi" *.html

The problem is that wildcards does not work with my
perl on Windows XP. I am not sure if it works in Unix either.

The proper way is to use File::Find
It will recurse through any directory structure, visiting any file on the
way.
Then it is up to you to do whatever you want to for each file.

jue
 
M

Matt Garrish

Angel said:
"Jürgen Exner" <[email protected]> wrote in message

I said that:

"I would like to search and replace on many files located in
different subfolders of one main folder."

No that was just what you said you were trying to accomplish. You said your
problem was that:
I know it should be possible using regular expressions but
all my attempts failed. Can anyone help me ?

The "it" in the above sentence refers back to your original problem, which
was to wrap all the images in anchor tags.

No one's interested in trying to read your mind. Next time state your
problem clearly.

Matt
 
D

David K. Wall

Tad McClellan said:
But you can make "wildcards" work yourself by using:

perldoc -f glob

Good thing the OP is using winXP and not win98.

A friend of mine recently wanted a copy of a little utility program I had
written. He didn't have Perl (and didn't want or need it), so I packaged
the program with PAR under winXP and sent him the executable. When he ran
the exe on his win98 system, all he got from glob() was "Bad command or
filename", so I had to rewrite that portion of the program to use readdir()
and 'next unless ...'. What a PITA.

Maybe if I'd had a win98 system with which to create the exe it might have
worked?
 
A

Angel

Matt Garrish said:
No that was just what you said you were trying to accomplish. You said your
problem was that:


The "it" in the above sentence refers back to your original problem, which
was to wrap all the images in anchor tags.

No one's interested in trying to read your mind. Next time state your
problem clearly.

Matt


When I read my posting now I agree it did not state the problem clearly.
Next time I will try to be more specific. Thanks.
 
J

J. Romano

David K. Wall said:
Good thing the OP is using winXP and not win98.

A friend of mine recently wanted a copy of a little utility program I had
written. He didn't have Perl (and didn't want or need it), so I packaged
the program with PAR under winXP and sent him the executable. When he ran
the exe on his win98 system, all he got from glob() was "Bad command or
filename", so I had to rewrite that portion of the program to use readdir()
and 'next unless ...'.

Maybe if I'd had a win98 system with which to create the exe it might have
worked?

Dear David,

Actually, it wouldn't have made a difference which Win OS you
created it on; if the system the executable runs on doesn't have
ActiveState Perl, the glob() function probably won't work. Here's
why:

When a glob() function is encountered in a Perl script, ActiveState
Perl will run the "perlglob.exe" executable using the backtick
operator. The perlglob.exe program is usually installed in the
Perl\bin directory, so if ActiveState ActivePerl was never installed,
chances are that the perlglob.exe executable doesn't exist, causing
the backtick operator (and, later, the glob() function) to fail (which
is why you got the error: "Bad command or file name"). It will run
perfectly on the machine that created the PAR executable (because
perlglob.exe exists there), misleading people to believe that their
PAR executable only works on their operating systems.

We actually encountered this problem just a few days ago, and the
way that we got around this problem was to add the line:

use File::Glob ':glob';

and change all "glob" calls to "bsd_glob". After that, globbing
worked perfectly.

I suppose another simple solution would be to just copy the
perlglob.exe executable into a directory included in the $PATH of your
friend's computer.

So be careful when you run Perl code that uses the backtick
operator; if the executable (that you're running inside backticks)
doesn't exist on the machine that you run the script on, the script
certainly won't function correctly.

Hope this helps,

Jean-Luc Romano
 
B

Ben Morrow

When a glob() function is encountered in a Perl script, ActiveState
Perl will run the "perlglob.exe" executable using the backtick
operator.

As of 5.6 this is no longer true, and the File::Glob module is used on
all platforms.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top