Multiline Search-Replace With Perl One-liner

Discussion in 'Perl Misc' started by cibalo, Jan 12, 2012.

  1. cibalo

    cibalo Guest

    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
    cibalo, Jan 12, 2012
    #1
    1. Advertising

  2. cibalo

    cibalo Guest

    On Jan 12, 8:53 pm, Tad McClellan <> wrote:
    > 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
    cibalo, Jan 14, 2012
    #2
    1. Advertising

  3. cibalo <> wrote:
    >On Jan 12, 8:53 pm, Tad McClellan <> wrote:
    >> 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
    Jürgen Exner, Jan 14, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giles Bowkett

    nifty one-liner to search ObjectSpace?

    Giles Bowkett, Mar 6, 2007, in forum: Ruby
    Replies:
    12
    Views:
    227
    Robert Klemme
    Mar 6, 2007
  2. dale zhang
    Replies:
    8
    Views:
    406
    Tintin
    Nov 30, 2004
  3. Larry
    Replies:
    1
    Views:
    94
    Martien Verbruggen
    Feb 3, 2005
  4. h3r0

    one liner to search a file

    h3r0, Jun 9, 2005, in forum: Perl Misc
    Replies:
    7
    Views:
    78
    Rick Measham
    Jun 13, 2005
  5. h3r0

    one liner to search a file

    h3r0, Jun 9, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    72
    optional
    Jun 9, 2005
Loading...

Share This Page