Insert/Update using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert/Update using sed
# 1  
Old 07-11-2012
Insert/Update using sed

Hi,

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

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

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

Can someone please help?

Thanks

Last edited by methyl; 07-11-2012 at 09:28 AM.. Reason: please use code tags
# 2  
Old 07-11-2012
Code:
perl -lpe 's/<Date=\042.*?\042/<Date=\042'$(date +%Y%m%d)'\042/' Config.xml

# 3  
Old 07-11-2012
Code:
sed 's/date=\".*\"/date=\"20120711\"/g' config.xml


Last edited by methyl; 07-11-2012 at 09:28 AM..
# 4  
Old 07-11-2012
Quote:
Originally Posted by karthikeayan
sed 's/date=\".*\"/date=\"20120711\"/g' config.xml
That's not going to work. Please try the solution yourself before posting.
# 5  
Old 07-11-2012
Quote:
Originally Posted by elixir_sinari
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...

Code:
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"


Last edited by methyl; 07-11-2012 at 09:26 AM.. Reason: pleae use code tags
# 6  
Old 07-11-2012
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!

Code:
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">


Last edited by methyl; 07-11-2012 at 09:26 AM..
# 7  
Old 07-11-2012
Another way

Code:
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">

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert one table and update another with shellscript

I have a problem with my shell script. I want to insert data from file to table1(empty) and then, compare table1 with table2 and update some fields. The first part is correct, but the second part does not work. The only way it works is if after the first part I truncate table1 and run the script... (1 Reply)
Discussion started by: nika_mill
1 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Programming

MYSQL - trigger update on record insert or update

Right I have a MYSQL database with table1 with 3 columns, colA, colB and colC. I want to combine the data in the 3 columns into a 4th column names col_comb. Here's the SQL command that works: UPDATE table1 SET `col_comb` = CONCAT( `colA` , ' - ', `colB` , ', ', `colC` ); So now I want this... (5 Replies)
Discussion started by: barrydocks
5 Replies

4. Shell Programming and Scripting

Sed: how do I insert a \ in my replace

I'm in the process of being forward-thinking and finally converting my site's db to UTF-8. I've already done the UTF-8 conversion (on a copy for testing) and now I want to go through and convert html entities to their actual characters. I ran an entity decode on a mysqldump file but realized... (10 Replies)
Discussion started by: dheian
10 Replies

5. Shell Programming and Scripting

sed insert new line does not update file

Hi all, I have a file called "text.cpp" with the first line of "1" afterwards I tried in Ubuntu to type the following sed '12iasdasdasdasdsad' test.cpp > output.txt however when I tried to see the result of output.txt #cat output.txt 1 why is the line 12 is not updated to the... (6 Replies)
Discussion started by: peuceul
6 Replies

6. Shell Programming and Scripting

sed to insert a character

Hi all, I have a file like this Q8N302 21-84 Q8N157 15-45 Q99996 167-201 202-251 269-318 I want to insert a character or space if the line starts with a number and I used the command sed 's/^/#/' But in the output file, when it inserts this character, first digit in the number is... (2 Replies)
Discussion started by: kaav06
2 Replies

7. UNIX for Advanced & Expert Users

unix script for update or insert records from a file to a oracle table

Hi, I have delimited file(|). Sample data: 1|name|50009|DS24|0|12 2|name|30009|DS24|0|13 3|name|20409|DS24|0|14 4|name|20009|DS24|0|15 5|name|10009|DS24|0|16 I want to load this data into a oracle table (update and insert) Please help me the commands and also... (1 Reply)
Discussion started by: unihp1
1 Replies

8. UNIX and Linux Applications

how to create a logfile in unix after insert/update/delete from informix

I am running a Informix-4GL program to insert/update/delete on a particular table (say xxx). Now I want to create a logfile in unix which will store the affected data(say xxx.*) along with user information (say uname,IP address etc.) what command I should use from Informix-4GL ? I have tried the... (0 Replies)
Discussion started by: subhamukh
0 Replies

9. Shell Programming and Scripting

sed: how to insert tab?

Hi, I'm using the following to insert lines into file: sed ${rowNr}i'\ first row\ second row\ third row\ ' file.txt How can I add tab in front of each added line? "\t" or actual TAB does not seem to work? Thanks! (2 Replies)
Discussion started by: Juha
2 Replies

10. Shell Programming and Scripting

sed search and insert

how can i modify a file using sed when searching for a pattern and insert newline after the pattern? pattern example: $ ...(any characters)...$ $ may082001.../tmp/msg.001,,$ REPT CLEAR ALARMS ON UNIT 1 $ may082001.../tmp/msg.002,,$ UNIT 1 IN SERVICE into: $... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question