Multiline Search-Replace With Perl One-liner

C

cibalo

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
 
C

cibalo

I will ignore the restriction.
---------------------
#!/usr/bin/perl
use warnings;
use strict;

my $html = '<html>
<body>
<img src="public_html\images\Buster.jpg" height="250" width="128"
  alt="      /\ See above"
    title="      /\
See above" />
</body>
</html>
';

$html =~ s/src="([^"]+)"/ '"' . fix_slash($1) . '"'/ge;

print $html;

sub fix_slash {
    my($src) = @_;
    $src =~ tr!\\!/!;
    return $src;}

---------------------

Hello Tad McClellan,

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.

Your solution isn't that applicable to my case, becasue
1. I have many html files
2. All my html codings have to be included in your perl script.
3. Maybe your script can read my html files and modify the img src
path accordingly!

But Thank You Anyway!

Best Regards,
cibalo
 
J

Jürgen Exner

cibalo said:
I will ignore the restriction.
---------------------
#!/usr/bin/perl
use warnings;
use strict;

my $html = '<html>
<body>
<img src="public_html\images\Buster.jpg" height="250" width="128"
  alt="      /\ See above"
    title="      /\
See above" />
</body>
</html>
';

$html =~ s/src="([^"]+)"/ '"' . fix_slash($1) . '"'/ge;

print $html;

sub fix_slash {
    my($src) = @_;
    $src =~ tr!\\!/!;
    return $src;}

Thank you very much for replying to my post. What I need is a working
solution,

That sounds like a job offer to me. This NG is not about job offers.
Your solution isn't that applicable to my case, becasue
1. I have many html files

Yes. So? Why does that make Ted's function inapplicable?
2. All my html codings have to be included in your perl script.

How so? Ted's script suggested nothing of that sort.
3. Maybe your script can read my html files and modify the img src
path accordingly!

Guess what? Ted's script is self-contained such that anyone can run and
test the code as is without any further external data or modifications.
That is the most desireable way to post code because nobody else has
your files but you.
Did you even look at his code? Did you even understand the most basic
structure? Or are you really unable to identify the key element that
does all the work and copy-n-paste that piece into your own program?
If that is your problem, then indeed you may want to post a job offer,
but please do so it in a more appropriate NG.
But Thank You Anyway!

Thank you for your consideration.

jue
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top