FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 11-29-2009, 10:53 AM
Ray Parrish
 
Default Regular Expression Puzzle

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@RaysComputer:~/test$ FileContents=`cat Test.html`
ray@RaysComputer:~/test$ SearchTerm="<div><a href="index.html">Ray's
Links Home</a>*</div>"
ray@RaysComputer:~/test$ ReplaceTerm="<p><a href="index.html">Ray's
Links Home</a>*</p>"
ray@RaysComputer:~/test$
FileContents=${FileContents/$SearchTerm/$ReplaceTerm}
ray@RaysComputer:~/test$ echo "$FileContents"
<p><a href="index.html">Ray's Links Home</a>*</p>
ray@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?

Thanks for any help you can be, 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



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-29-2009, 11:26 AM
Alan McKay
 
Default Regular Expression Puzzle

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




--
“Don't eat anything you've ever seen advertised on TV”
- Michael Pollan, author of "In Defense of Food"

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-29-2009, 12:37 PM
Ray Parrish
 
Default Regular Expression Puzzle

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.

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



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-29-2009, 12:39 PM
Loc Greni
 
Default Regular Expression Puzzle

2009/11/29 Alan McKay <alan.mckay@gmail.com>:
> 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

even simpler:

sed "s/<div>/<p>/g" file | sed "s,</div>,</p>,g" > newfile

Variants:

(one sed, two regexps)
sed -e "s/<div>/<p>/g" -e "s,</div>,</p>,g" file > newfile

(only one regexp)
sed "s,<(/?)div>,<1p>,g" file > newfile

(shown only for the last one, but it can be used for any,
change in place)
sed -i "s,<(/?)div>,<1p>,g" file
in that case the file "file" is changed, non new file is created.

Loc

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-29-2009, 01:03 PM
Werner Schram
 
Default Regular Expression Puzzle

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

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-29-2009, 01:53 PM
Alan McKay
 
Default Regular Expression Puzzle

> 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.

You need a different approach to that problem - you should be using
generated pages

> 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.

Oh I'm sure it can be done, but I think you are going about this the
wrong way. What you are doing is very prone to errors.


--
“Don't eat anything you've ever seen advertised on TV”
- Michael Pollan, author of "In Defense of Food"

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-29-2009, 02:26 PM
Hal Burgiss
 
Default Regular Expression Puzzle

On Sun, Nov 29, 2009 at 09:53:09AM -0500, Alan McKay wrote:
> > 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.
>
> You need a different approach to that problem - you should be using
> generated pages

Even better, if I read the intent right as being a change to the look/feel of
the document, is to use css, which is around for just the reason. Apply a
'class' to each like element, put a definition in the site's stylesheet, and
then when you want a global change, you can do it with a one line edit to that
css rule.

Also, if there is some consistency to the markup, you can use jQuery to
dynamically to it all:

<script type="text/javascript">
$("div a").css({'colorurple','background':'#ccc','fo nt-weight':'bold'});
</script>

which would handle any anchor tag that is a child of a div tag. jQuery will be
more useful though with good use of css selectors in the markup itself.

--
Hal

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-29-2009, 06:32 PM
Andrew Kane
 
Default Regular Expression Puzzle

It reads to me as though he's trying to change semantic elements in
the markup rather than look-and-feel changes.
My suggestion would be instead to use the "id" attribute for each of
the divs in question (in case he later wants to address them
separately for css or search / replace) and to use sed as discussed
above to search & replace- except that I would possibly recommend to
include both lines in the file and just comment out the one I don't
want.
As you say, he should be using generated pages.

On Sun, Nov 29, 2009 at 7:26 AM, Hal Burgiss <hal@burgiss.net> wrote:
> On Sun, Nov 29, 2009 at 09:53:09AM -0500, Alan McKay wrote:
>> > 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.
>>
>> You need a different approach to that problem - you should be using
>> generated pages
>
> Even better, if I read the intent right as being a change to the look/feel of
> the document, is to use css, which is around for just the reason. Apply a
> 'class' to each like element, put a definition in the site's stylesheet, and
> then when you want a global change, you can do it with a one line edit to that
> css rule.
>
> Also, if there is some consistency to the markup, you can use jQuery to
> dynamically to it all:
>
> <script type="text/javascript">
> * * * *$("div a").css({'colorurple','background':'#ccc','fo nt-weight':'bold'});
> </script>
>
> which would handle any anchor tag that is a child of a div tag. jQuery will be
> more useful though with good use of css selectors in the markup itself.
>
> --
> Hal
>
> --
> ubuntu-users mailing list
> ubuntu-users@lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-29-2009, 08:11 PM
Hal Burgiss
 
Default Regular Expression Puzzle

On Sun, Nov 29, 2009 at 11:32:33AM -0800, Andrew Kane wrote:
> It reads to me as though he's trying to change semantic elements in
> the markup rather than look-and-feel changes.

I was thinking within the context given either div or p could be sematically
correct, and the reasoning for changing one to the other was because one
renders differently. But then again it might just be an intellectual exercise
for learning regexes.

--
Hal

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-29-2009, 08:13 PM
Ray Parrish
 
Default Regular Expression Puzzle

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



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 

Thread Tools




All times are GMT. The time now is 02:52 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org