Computing.Net > Forums > Programming > How to replace a string using sed?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to replace a string using sed?

Reply to Message Icon

Name: LZuser
Date: February 25, 2009 at 23:18:20 Pacific
OS: Unix
Subcategory: Batch
Comment:

Hello,

Can someone tell me how to clean a string inside of "" using sed?
My data is in a file: a.xml
<Contact type="Email" guid="37126FB6-F92C-4A16-8D04-39DEE50CFD16">internal@abc.com</Contact>
<Contact type="Work Phone" guid="7D5CF797-4598">650 111 2345</Contact>

I need to change it as:
<Contact type="Email" guid="">out@abc.com</Contact>
<Contact type="Work Phone" guid="">650 111 2345</Contact>

Thanks a lot.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: February 26, 2009 at 08:59:28 Pacific
Reply:

In order to null out the guid using sed, the easiet way is to use a regular expression that replaces everything but a certain character within the double quotes. That character can NOT be part of the guid. I chose to use the right bracket, ]. The * means, use zero or more repeating characters and is required:

sed 's/guid="[^]]*"/guid=""/g' a.xml


0

Response Number 2
Name: LZuser
Date: February 26, 2009 at 10:09:43 Pacific
Reply:

Thank you so much for your quick response. I run this sed. But I lost the containts of after guid. The result shows:
<Contacts>
<Contact type="Email" guid=
<Contact type="Work Phone" guid=
</Contacts>
Can you help me more? Many many thanks.


0

Response Number 3
Name: nails
Date: February 26, 2009 at 10:18:21 Pacific
Reply:

Interesting. First, what version of Unix and what shell are you using? I'm using Solaris 9 with the korn shell and it works fine.

Second, perhaps it's a problem with your version of sed dealing with "". What happens if you do somehting like this?

sed 's/guid="[^]]*"/guid="X"/g' a.xml

Does it replace your guid value with X


0

Response Number 4
Name: LZuser
Date: February 26, 2009 at 11:01:34 Pacific
Reply:

Thank you so much my griend.
It replace with X. But after X, all are cut off.
Result:
<Contacts>
<Contact type="Email" guid=X
<Contact type="Work Phone" guid=X
</Contacts>

Expected is:
<Contacts>
<Contact type="Email" guid=X>abc@new.com</Contact>
<Contact type="Work Phone" guid=X>650 111 2345</Contact>
</Contacts>

It would be great, if it only replace guid=" " and keep the rest.
Again, many thanks


0

Response Number 5
Name: LZuser
Date: February 26, 2009 at 11:16:17 Pacific
Reply:

Hi friend,
I use GNU utilities for Win32. Is this may cause problem. Do you know any other window version utility?
Many thanks!!!


0

Related Posts

See More



Response Number 6
Name: nails
Date: February 26, 2009 at 11:52:45 Pacific
Reply:

First, I do not expect this to work, but maybe your version of sed doesn't like ^]. You might try using another character like "S" and see if that works:

sed 's/guid="[^S]*"/guid=""/g' a.xml

but I don't expect it to.

Second, I'm not familiar with the WIN32 GNU utilities. For window's I use the MKS unix tool suite available at:

http://www.mks.com

I tried the sed command with MKS and it works fine.

While i highly recommend it, MKS is not free. Probably the most well known free unix toolkit for Windows is cygwin. It's available for download on the web many places. Here are two:

http://www.cygwin.com/
http://cygwin.en.softonic.com/

Sorry, but I haven't used cygwin in years.

Tonight, when I get home, I'll try this on my Linux system and see what happens.



0

Response Number 7
Name: LZuser
Date: February 26, 2009 at 15:01:32 Pacific
Reply:

I truly appreciate your generous help. I'm going to try it on Solaris and MKS. I'll let you know the results.
Please let me know your Linux results.

Many thanks again and have a good day.


0

Response Number 8
Name: LZuser
Date: February 26, 2009 at 15:53:59 Pacific
Reply:

Hi Friend,

Great news!!!
The sed works in win32 as following:

sed s/guid=\".*\"/guid=\"\"/g a.xml>out.xml

Thanks you so much for your time and patient.


0

Sponsored Link
Ads by Google
Reply to Message Icon

batch file compare creati... Google Chrome inserts har...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: How to replace a string using sed?

How to seach *.mp3 etc using batch www.computing.net/answers/programming/how-to-seach-mp3-etc-using-batch/15950.html

how to replace multiple strings www.computing.net/answers/programming/how-to-replace-multiple-strings/18191.html

How to create a text file using FSO www.computing.net/answers/programming/how-to-create-a-text-file-using-fso/10319.html