Regular Expression Puzzle

Werner Schram wrschram at gmail.com
Sun Nov 29 14:03:32 UTC 2009


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




More information about the ubuntu-users mailing list