How to replace a string using sed?

February 25, 2009 at 23:18:20
Specs: Unix

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"></Contact>
<Contact type="Work Phone" guid="7D5CF797-4598">650 111 2345</Contact>

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

Thanks a lot.

See More: How to replace a string using sed?

Report •

February 26, 2009 at 08:59:28
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

Report •

February 26, 2009 at 10:09:43
Thank you so much for your quick response. I run this sed. But I lost the containts of after guid. The result shows:
<Contact type="Email" guid=
<Contact type="Work Phone" guid=
Can you help me more? Many many thanks.

Report •

February 26, 2009 at 10:18:21
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

Report •

Related Solutions

February 26, 2009 at 11:01:34
Thank you so much my griend.
It replace with X. But after X, all are cut off.
<Contact type="Email" guid=X
<Contact type="Work Phone" guid=X

Expected is:
<Contact type="Email" guid=X></Contact>
<Contact type="Work Phone" guid=X>650 111 2345</Contact>

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

Report •

February 26, 2009 at 11:16:17
Hi friend,
I use GNU utilities for Win32. Is this may cause problem. Do you know any other window version utility?
Many thanks!!!

Report •

February 26, 2009 at 11:52:45
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:

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:

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.

Report •

February 26, 2009 at 15:01:32
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.

Report •

February 26, 2009 at 15:53:59
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.

Report •

Ask Question