Please help with search/replace Perl string!

Discussion in 'Perl Misc' started by Ron M., Jan 5, 2004.

  1. Ron M.

    Ron M. Guest

    I do a lot of web page maintenance, which includes frequently updating
    some old sites with large numbers of static, text-based pages in a
    UNIX or AIX environment. I use several Perl "tools" in my work,
    although I'm not a Perl programmer.

    Task: use the following Perl code to replace all occurrences of the
    string, "this is the old string" with "this is the new string" and
    create backup files with the ".back" suffix. Do this with all files
    whose filename contains the string, ".html."

    perl -pi.back -e 's/this is the old string/this is the new string/g'
    *.html*

    Problem: if, in the actual file, the string, "this is the old string"
    is broken over two lines, the Perl code won't work! It has to be all
    on the same line, or the text won't be replaced, and I have to go back
    through each file one at a time and manually replace it in vi, which
    is kinda self-defeating.

    If it's possible, can somebody please change this Perl string so it
    will catch those broken strings?

    (please reply in the newsgroup)

    Thanks a HEAP,
    Ron M.
     
    Ron M., Jan 5, 2004
    #1
    1. Advertising

  2. Ron M.

    Ben Morrow Guest

    (Ron M.) wrote:
    > Task: use the following Perl code to replace all occurrences of the
    > string, "this is the old string" with "this is the new string" and
    > create backup files with the ".back" suffix. Do this with all files
    > whose filename contains the string, ".html."
    >
    > perl -pi.back -e 's/this is the old string/this is the new string/g'
    > *.html*
    >
    > Problem: if, in the actual file, the string, "this is the old string"
    > is broken over two lines, the Perl code won't work! It has to be all
    > on the same line, or the text won't be replaced, and I have to go back
    > through each file one at a time and manually replace it in vi, which
    > is kinda self-defeating.
    >
    > If it's possible, can somebody please change this Perl string so it
    > will catch those broken strings?


    If strings will only be broken at spaces:
    perl -0777pi.back \
    -e's/this[ \n]is[ \n]the[ \n]old[ \n]string/this is the new string/g' \
    *.html*

    If you want to keep the newlines:

    perl -0777pi.back \
    -e's/this([ \n])is([ \n])the([ \n])old([ \n])string/this$1is$2the$3new$4string/g' \
    *.html*

    To allow (and preserve) any amount of space between words:

    perl -0777pi.back \
    -e's/this(\s+)is(\s+)the(\s+)old(\s+)string/this$1is$2the$3new$4string/g' \
    *.html*

    If the lines can break anywhere, and you don't need to keep the
    breaks:

    perl -0777pi.back \
    -e'my $str = "this is the old string"' \
    -e'my $re = join "\n?", split //, $str' \
    -e's/$re/this is the new string/g' \
    *.html*

    All of these are untested.

    Ben

    --
    Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
    From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
    (Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
    /Alcestis/) [ flame, and falls out of sight. ]
     
    Ben Morrow, Jan 5, 2004
    #2
    1. Advertising

  3. (Ron M.) writes:

    > Task: use the following Perl code to replace all occurrences of the
    > string, "this is the old string" with "this is the new string" and
    > create backup files with the ".back" suffix. Do this with all files
    > whose filename contains the string, ".html."
    >
    > perl -pi.back -e 's/this is the old string/this is the new string/g'
    > *.html*
    >
    > Problem: if, in the actual file, the string, "this is the old string"
    > is broken over two lines, the Perl code won't work! It has to be all
    > on the same line, or the text won't be replaced, and I have to go back
    > through each file one at a time and manually replace it in vi, which
    > is kinda self-defeating.


    You can tell Perl to treat the whole file as a single string by
    undefining $/. Obviously you don't want to do this for large files.

    This will still not find the pattern split over new-line boundaries
    because that wouldn't match the literal spaces. You need to say that
    your target pattern can match arbitrary whitespace between words.

    perl -pi.back -e 'BEGIN { undef $/ }; s/this\s+is\s+the\s+old\s+string/this is the new string/g'

    ISTR there's a way to undef $/ using the -l switch but I can't find it
    in perlrun.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
     
    Brian McCauley, Jan 5, 2004
    #3
  4. Ron M.

    gnari Guest

    "Brian McCauley" <> wrote in message
    news:...
    > (Ron M.) writes:
    > perl -pi.back -e 'BEGIN { undef $/ };

    s/this\s+is\s+the\s+old\s+string/this is the new string/g'
    >
    > ISTR there's a way to undef $/ using the -l switch but I can't find it
    > in perlrun.


    under -0 ?

    gnari
     
    gnari, Jan 5, 2004
    #4
  5. Ron M. <> wrote:


    > Task: use the following Perl code to replace all occurrences of the
    > string, "this is the old string" with "this is the new string" and


    > Problem: if, in the actual file, the string, "this is the old string"
    > is broken over two lines,



    Then it does not meet the condition set out in the Task.


    > the Perl code won't work!



    Poor specifications often lead to poor implementations.


    > It has to be all
    > on the same line, or the text won't be replaced,



    It if is not all on the same line, then it is not the string
    "this is the old string", it is something else.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 5, 2004
    #5
    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. KK
    Replies:
    2
    Views:
    728
    Big Brian
    Oct 14, 2003
  2. Schif Schaf
    Replies:
    12
    Views:
    1,316
    Anthra Norell
    Feb 8, 2010
  3. Prasad S
    Replies:
    2
    Views:
    260
    Dr John Stockton
    Aug 27, 2004
  4. walala
    Replies:
    21
    Views:
    225
    John W. Krahn
    Sep 23, 2003
  5. Replies:
    12
    Views:
    204
Loading...

Share This Page