Regular Expression Puzzle

Loïc Grenié loic.grenie at gmail.com
Mon Nov 30 07:11:41 UTC 2009


2009/11/29 Ray Parrish <crp at cmc.net>:
> Werner Schram wrote:
>> In the mean time, I think this one is what you are looking for:
>>
>> sed "s,<div><a href=\"index.html\">Ray's Links
>> Home</a>\(.*\)</div>,<p><a href=\"index.html\">Ray's Links
>> Home</a>\1</p>,g" file
>>
>> the inportant part is the \(.*\) in the search pattern and the \1 in the
>> replace pattern. The brackets mean that you are grouping something to
>> which you can reference back from the replace pattern using \1. The .
>> matches any character and * means that you expect 0 or more of the
>> previous character.
>>
>> Werner
>>
> Here is the result of running that command. I don't have a clue what is
> wrong with the syntax, as I've never used sed before.
>
> ray at RaysComputer:~/test$ sed "s,<div><a href=\"index.html\">Ray's Links
> Home</a>\(.*\)</div>;<p><a href=\"index.html\">Ray's Links
> Home</a>\1</p>,g" Test.html
> sed: -e expression #1, char 40: unterminated `s' command
> ray at RaysComputer:~/test$
>
> Thanks for helping, I would like to get this to work, so I guess I
> better locate the sed documents and start studying them.

    The problem are the \ and " in the " because both \ and " are
  special to " (the ` and $ are also special in case you wondered).
  You should try

sed 's,<div><a href=\"index.html\">Ray'\''s Links
Home</a>\(.*\)</div>;<p><a href=\"index.html\">Ray'\''s Links
Home</a>\1</p>,g' Test.html

   (the ' is special to ' so that I escaped them).

          Loïc




More information about the ubuntu-users mailing list