Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multiline Search-Replace With Perl One-liner

39 views
Skip to first unread message

cibalo

unread,
Jan 12, 2012, 4:27:20 AM1/12/12
to
Hello,

I have migrated my free website to my linux web server recently. I
have to re-code all my htm files, of which they were created under
Windows.

I need to make a search and replace with Perl One-liner. I want to
search/replace backward slash to forward slash only in the relative
image path for all <img> tags, which may span over multiple lines.
So the <img> tag may like this:
<html>
<body>
<img src="public_html\images\Buster.jpg" height="250" width="128"
alt=" /\ See above"
title=" /\
See above" />
</body>
</html>

Sadly I haven't done RegEx searching in a while now. I'd be very
thankful for some help.

Thank you very much in advance!!!

Best Regards,
cibalo

James Waldby

unread,
Jan 13, 2012, 3:25:37 AM1/13/12
to
...

I imagine there is some better way of handling the problem than with
perl, and almost certainly better perl code exists than that below,
but in any case, the following converts back-slashes within 'img src='
paths occurring singly on single lines; eg, it converts 3rd line to:
<img src="public_html/images/Buster.jpg" height="250" width="128"
and leaves other lines as is.

perl -pe 'm/(img src="[^"]*)/; $path=$1; $path =~ s|\\|/|g; s/(img src="[^"]*)/$path/'

It isn't clear to me what your multi-line case would look like, etc.
Note, if some lines have more than one 'img src=' paths on them, run
the script more than once.

--
jiw

Ed Morton

unread,
Jan 13, 2012, 7:52:09 AM1/13/12
to
On 1/12/2012 3:27 AM, cibalo wrote:
> Hello,
>
> I have migrated my free website to my linux web server recently. I
> have to re-code all my htm files, of which they were created under
> Windows.
>
> I need to make a search and replace with Perl One-liner.

1) why does it need to be perl?
2) why does it need to be a one-liner?

Ed.
Message has been deleted

cibalo

unread,
Jan 13, 2012, 10:03:34 PM1/13/12
to
On Jan 13, 4:25 pm, James Waldby <n...@valid.invalid> wrote:

> It isn't clear to me what your multi-line case would look like, etc.
> Note, if some lines have more than one 'img src=' paths on them, run
> the script more than once.

Hello James Waldby, Ed Morton and houghi,

Thank you very much for replying to my post. What I need is a working
solution, be it a one-liner or perl/sed/awk/bash script.

My multi-line case has only one 'img src=' path.

That is not my home work assignment.

Best Regards,
cibalo

Keith Keller

unread,
Jan 14, 2012, 12:49:31 AM1/14/12
to
On 2012-01-14, cibalo <cib...@gmx.co.uk> wrote:
>
> My multi-line case has only one 'img src=' path.

Speaking of "multi", it is considered incredibly rude to multipost, as
you have done, here and to comp.lang.perl.misc. If you must reach
multiple groups, crosspost instead of multiposting.

--keith


--
kkeller...@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information

Message has been deleted
0 new messages