Regular Expression Puzzle

Ray Parrish crp at cmc.net
Sun Nov 29 22:04:19 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
>   
Success!!! It was the line feeds introduced by email that kept it from 
working. Here is the output -

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

<p><a href="index.html">Ray's Links Home</a> / <a 
href="Science.html">Science</a> / Biology</p>
<p><a href="index.html">Ray's Links Home</a> / <a href="Web 
Publishing.html">Web Publishing</a> / Web Editors</p>

ray at RaysComputer:~/test$

Now all I have to do is figure out how to incorporate that into my multi 
document editor program. 8-)

Later, Ray Parrish




-- 
The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com






More information about the ubuntu-users mailing list