Regular Expression Puzzle
Ray Parrish
crp at cmc.net
Sun Nov 29 21:13:00 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
>
When I get done with my email, I'm going to try that syntax you used
there without sed to see if it works. If it will work that way, I can
use it with my new block oriented, multi-document search and replace
program I just finished writing in bash, with the help of Xdialog for
some GUI support.
In it I assign a SearchTerm, and a ReplaceTerm variable which can
contains lines of text, and use them in the bash replace expression like
so -
FileContents=`cat file.html`
FileContents=${FileContents/$SearchTerm/$ReplaceTerm}
If I can just edit the SearchTerm, and ReplaceTerm to add the \(.*\) and
the /1 in each respectively, and it works, then it will be a real boon
to my ability to change my web sites around.
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