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")
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!
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 ----------
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