Regular Expression Puzzle

Kenneth Loafman kenneth at loafman.com
Sun Nov 29 21:27:39 UTC 2009


Ray Parrish wrote:
> Hello,
> 
> I am trying to fathom a way to include a regular expression in a Search 
> Term, and a Replace Term so that I can replace text strings that vary 
> somewhat. For example, I have the following two lines in Test.html
> 
> <div><a href="index.html">Ray's Links Home</a> / <a 
> href="Science.html">Science</a> / Biology</div>
> <div><a href="index.html">Ray's Links Home</a> / <a href="Web 
> Publishing.html">Web Publishing</a> / Web Editors</div>
> 
> I would like to replace the <div>, and </div> tags with <p></p> for 
> instance. What I did is -
> 
> ray at RaysComputer:~/test$ FileContents=`cat Test.html`
> ray at RaysComputer:~/test$ SearchTerm="<div><a href=\"index.html\">Ray's 
> Links Home</a>*</div>"
> ray at RaysComputer:~/test$ ReplaceTerm="<p><a href=\"index.html\">Ray's 
> Links Home</a>*</p>"
> ray at RaysComputer:~/test$ 
> FileContents=${FileContents/$SearchTerm/$ReplaceTerm}
> ray at RaysComputer:~/test$ echo "$FileContents"
> <p><a href="index.html">Ray's Links Home</a>*</p>
> ray at RaysComputer:~/test$
> 
> As you can see, it did not turn out quite how i wanted it to. I want to 
> preserve everything between the <div></div> tags, and replace them with 
> paragraph tags for this experiment.
> 
> I thought the * in the SearchTerm, and ReplaceTerm would act as a 
> wildcard, and preserve what was there, but it replaces it all with itself.
> 
> Can anybody give me some guidance here?

1) don't try to do it in bash, the syntax is arcane at its best.
2) use something like sed -f and a regex file to do this like the
attached sed.example.sh and .log.  I tried to quote it here, but the
line wrap makes things quite unreadable.

...Ken



-------------- next part --------------
A non-text attachment was scrubbed...
Name: sed-example.sh
Type: application/x-sh
Size: 392 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20091129/76dc8c8e/attachment.sh>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sed-example.log
Type: text/x-log
Size: 461 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20091129/76dc8c8e/attachment.bin>


More information about the ubuntu-users mailing list