Insert/Update using sed

Hi,

I have a xml file (Config.xml) with following entry

<Date="" Node1="50" Groups="20">

Now I want to use sed to insert/update the Date field with the latest date say - 20120711. I can't use a simple replace command becuase the Date field could be blank ("") or sometimes could have value in it ("20110710")

Tried below sed command but failed.

sed -e 's/Date=.*"/Date=20120711/' Config.xml

Can someone please help?

Thanks

perl -lpe 's/<Date=\042.*?\042/<Date=\042'$(date +%Y%m%d)'\042/' Config.xml
sed 's/date=\".*\"/date=\"20120711\"/g' config.xml

That's not going to work. Please try the solution yourself before posting.

oopss.. sry, but i tried in my server, bash shell, it is working...

user@server:/tmp/pce> cat config.xml
date=""
date="20110607"
date=""
user@server:/tmp/pce> sed 's/date=\".*\"/date=\"20120711\"/g' config.xml
date="20120711"
date="20120711"
date="20120711"

The problem is that the sed does not work with the sample data (even after changing date to Date). It replaces the pattern between the first and last double quote character, such that the other two fields disappear!

echo '<Date="19991212" Node1="50" Groups="20">' |sed 's/Date=\".*\"/Date=\"20120711\"/g'
<Date="20120711">

echo '<Date="" Node1="50" Groups="20">' |sed 's/Date=\".*\"/Date=\">
<Date="20120711">

Another way

echo '<Date="" Node1="50" Groups="20">' | awk '{$1=x}1' x='<Date="20110710"'
<Date="20110710" Node1="50" Groups="20">

echo '<Date="20110202" Node1="50" Groups="20">' | awk '{$1=x}1' x='<Date="20110710"'
<Date="20110710" Node1="50" Groups="20">

Sorry, I should have made it clear in my first post.

The xml itself is a big file, so unfortunately I can't try the echo option. Also the insert/update should be committed using the solution.

To re-iterate:

<Date="" Node1="50" Groups="20">

is a part of a big xml file Config.xml and I am trying to insert/update the Date field and commit the changes.

Reason for Insert/update option
1) Insert: The Date field could be blank (Date="")
2) Update: xml could already have a date in it ((Date="20120610")). So the command should replace it.

something like this

#! /bin/ksh
dt=20120701
sed -e 's/Date=.*"/Date=$dt/' Config.xml

---------- Post updated 07-12-12 at 04:14 AM ---------- Previous update was 07-11-12 at 07:42 AM ----------

Hi,

Can someone please help with my query?

Thanks

I don't think anyone has suggested to use echo to solve your problem. What they have done is to demonstrate methods to tackle your problem using the only data you have provided.

Anyway, you can use something like:

sed '/<Date=.*Node1=.*Groups.*>/s/<Date="[^"]*"/<Date="20120712"/g' inputfile > temp
mv temp inputfile

Sorry if you read the earlier post, you could see solutions based on 'echo'. That's why I corrected my statement in the later post saying its a big xml file and 'echo' doesn't suit.

Anyways thanks for the new solution with <Date="[^"]*"

Cheers
Vivek

---------- Post updated at 08:29 AM ---------- Previous update was at 06:06 AM ----------

For the benefit of users of the forum, here is my final solution

sed -i 's/Date="[^"]*"/Date="20120712"/' Config.xml

Cheers
Vivek