Regular Expression Puzzle

Ray Parrish crp at
Sun Nov 29 21:55:10 UTC 2009

Werner Schram wrote:
> Ray Parrish wrote:
>> Alan McKay wrote:
>>> Dude, that's a bit of a convoluted way to go about it.
>>> How about keeping it simple?
>>> cat file | sed "s/<div>/<p>/g" | sed "s/<\/div>/<\/p>/g" > newfile
>> Hello,
>> I tried that command, and it does indeed change the <div>'s to <p>'s, 
>> but it does it across the entire file, and I do not want to replace 
>> every <div> in the file with paragraph tags.
>> I need the ability to change certain blocks of text that are similar but 
>> different in different files, and I would like a command that handles 
>> all variations on the basic theme of it starts with a certain string, 
>> varies in the middle, then is the same on the end of the string again.
>> Those sample lines iI provided are from the navigation links I put at 
>> the top of all of my web pages, and it would be nice to do a broadcast 
>> change to update, or change their appearance instead pf having to edit 
>> every single file by hand, which takes hours.
>> Well, I'm back to reading up on regular exprssions to see if there is 
>> some possible way to make a broadcast change through all of my pages to 
>> change the appearance of the navigation links.
> Doing some reading is a good idea, regular expression can be extremely 
> usefull. And being able to write undecipherable code that actually does 
> what you want greatly boosts your l33t score :)
> 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.

Later, Ray Parrish

The Future of Technology.
Ray's Links, a variety of links to usefull things, and articles by Ray.
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.

More information about the ubuntu-users mailing list