Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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

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.

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

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

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

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

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.

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.

![]() |
batch file compare creati...
|
Google Chrome inserts har...
|

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