The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Operating Systems
> UNIX Help
|
Sed Problem
Discuss Sed Problem in the UNIX Help forum on Dev Shed. Sed Problem UNIX Help forum discussing the Unix Operating System and all variants including Irix, Solarix, and AIX. Unix was designed as a true multi-user operating system.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 12th, 2003, 11:15 PM
|
|
Registered User
|
|
Join Date: Nov 2003
Posts: 22
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Sed Problem
Hi, I am face with a problem with sed command here. I wan to find a string and replace it within a file.
Eg.
from
<a href="BuildPage.cgi?body=index.html">
change to
<a href=BuildPage.cgi?body=index.html">
what is the code tat i should do? thanks for helping..
|

November 13th, 2003, 09:30 AM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 137
Time spent in forums: 5 m 37 sec
Reputation Power: 0
|
|
|
echo <string> | sed 's/"//'
But I guess you want to delete the second " too:
echo <string> | sed 's/"//g'
|

November 13th, 2003, 06:50 PM
|
|
Registered User
|
|
Join Date: Nov 2003
Posts: 22
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Thanks ya~
yupe actually i wan to replace both double quotes here..
But, the file i wan to change got lots of double quotes and i just want to change the double quotes of the href action. So, how to do that ler? is it like tat
sed 's/href=\"*.*\"/href=*.*/'
|

November 14th, 2003, 02:59 AM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 137
Time spent in forums: 5 m 37 sec
Reputation Power: 0
|
|
|
You need a good regular expression to do this.
I can give an example, but it depends on how the html page was written. More specific: how the <a href......> line is written.
- Is it by itself on a line,
- Do you use title="..." in the href tag,
- Are there multiple html statements on 1 line,
- etc
Just 3 of many examples:
<a href="BuildPage.cgi?body=index.html">
<a href="BuildPage.cgi?body=index.html" title="Build Page">Name</a>
<a href="BuildPage.cgi?body=index.html">Name</a><p class="article">
For the above examples the following statement will do the trick:
cat <htm_file> | sed 's/a href="\(.*\)html"/a href=\1html/'
But you might need to change the regular expression for your specific needs.
Hope this helps.
|

November 14th, 2003, 03:37 AM
|
|
Registered User
|
|
Join Date: Nov 2003
Posts: 22
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
i try the code u type to me, but it doesn't work
the output i want should be
<a href=BuildPage.cgi?body=index.html>
but the output i get is
<a href=html>
the word after the href and b4 the html all gone.
beside this, sometimes the href not always end with html, for example
<a href="BuildPage.cgi?body=index.html#adac">
so, i wan to eliminate the double quotes of the code above too~
do u hv any better solution? Thanks you
|

November 14th, 2003, 04:59 AM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 137
Time spent in forums: 5 m 37 sec
Reputation Power: 0
|
|
|
Sorry, but the solution I gave does work:
$ cat somepage.html
<a href="BuildPage.cgi?body=index.html">
<a href="BuildPage.cgi?body=index.html" title="Build Page">Name</a>
<a href="BuildPage.cgi?body=index.html">Name</a><p class="article">
$ cat somepage.html | sed 's/a href="\(.*\)html"/a href=\1html/'
<a href=BuildPage.cgi?body=index.html>
<a href=BuildPage.cgi?body=index.html title="Build Page">Name</a>
<a href=BuildPage.cgi?body=index.html>Name</a><p class="article">
As you can see, only the " around "BuildPage.cgi?body=index.html" are stripped all the rest stays the same.
Did you make a typo??
As for the ......html#sometag".... You need to adjust the regular expression for that:
This will take care of the example you gave (and still works with the 3 example lines shown above):
sed 's/a href="\(.*\)html[#]*[a-z]*"/a href=\1html/'
Last edited by druuna : November 14th, 2003 at 06:29 AM.
|

November 16th, 2003, 06:59 PM
|
|
Registered User
|
|
Join Date: Nov 2003
Posts: 22
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Thanks for ur helping, now i already solve the first problem. Now, the second problem is the typo.
from the code that u given to me, it will remove the typo behind. but actually i wan the typo to remain there.
for example,
<a href="BuildPage.cgi?body=index.html#abc">
change to
<a href=BuildPage.cgi?body=index.html#abc>
|

November 17th, 2003, 04:27 AM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 137
Time spent in forums: 5 m 37 sec
Reputation Power: 0
|
|
|
Maybe you misunderstood me.
Both examples I gave will do what you want.
The second one will take care of the 'html#abc' stripping.
|

November 17th, 2003, 07:06 PM
|
|
Registered User
|
|
Join Date: Nov 2003
Posts: 22
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Thanks druuna, I solve the problem liao.. Thanks for ur guidance and helping~
It does help me a lots. i appreciate it very much~
all the best~
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|