Find and replace in xml


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace in xml
# 1  
Old 03-16-2014
Find and replace in xml

Hi All,

My XML file looks like below:

Code:
<logEvent xsi:type="logservice:LogEvent"  timestamp="1394713811052" severity="3" messageCode="TM_6228" message="Writing session output to log file ." user="" stacktrace="" service="" serviceType="IS" clientNode="dev" pid="712" threadName="DIRECTOR" context="">
</logEvent>
<logEvent xsi:type="logservice:LogEvent"  severity="3" messageCode="TM_6228" message="Writing session output to log file ." timestamp="1394713811052" user="" stacktrace="" service="" serviceType="IS" clientNode="" pid="712" threadName="DIRECTOR" context="">
</logEvent>
<logEvent xsi:type="logservice:LogEvent"  severity="3" messageCode="VAR_27028" message="Use override value" user="" stacktrace="" service="" serviceType="IS" clientNode="" pid="712" threadName="DIRECTOR" context="" timestamp="1394713811060">
</logEvent>

The timestamp value above is epoch time. I know how to convert epoch time to readbale format using date command. I want to replace the epoch timestamp value in the xml with readable date format[using date command]

The position of timestamp in the XML varies. How do i find and replace in this case. Please help in resolving this.

Regards,
# 2  
Old 03-16-2014
Try:
Code:
perl -slpe 'use POSIX;/(timestamp=")([^"]+)/;$ts=strftime "$fmt", gmtime $2/1000;s/(timestamp=")([^"]+)/$1$ts/' -- -fmt="%Y-%m-%d %H:%M" file.xml

Replace the format at the end of that command with whatever format you would pass to date.
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 03-16-2014
If awk is fine for you try this
Code:
$ awk -F 'timestamp="' 'NF>1{s=$2;sub(/".*/,"",s);sub(s,strftime("%d-%h-%Y %H:%M:%S",s/1000))}1' file

<logEvent xsi:type="logservice:LogEvent"  timestamp="13-Mar-2014 18:00:11" severity="3" messageCode="TM_6228" message="Writing session output to log file ." user="" stacktrace="" service="" serviceType="IS" clientNode="dev" pid="712" threadName="DIRECTOR" context="">
</logEvent>
<logEvent xsi:type="logservice:LogEvent"  severity="3" messageCode="TM_6228" message="Writing session output to log file ." timestamp="13-Mar-2014 18:00:11" user="" stacktrace="" service="" serviceType="IS" clientNode="" pid="712" threadName="DIRECTOR" context="">
</logEvent>
<logEvent xsi:type="logservice:LogEvent"  severity="3" messageCode="VAR_27028" message="Use override value" user="" stacktrace="" service="" serviceType="IS" clientNode="" pid="712" threadName="DIRECTOR" context="" timestamp="13-Mar-2014 18:00:11">
</logEvent>

This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 03-19-2014
Thanks bartus11 and Akshay Hegde.. both solutions worked Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find replace text in xml file on the fly

Dear Unix guru, I have a .XML file which is being used to load data to oracle. This file comes on unix box and one of the tag in xml is oracle key word. I want to find that tag and replace with new tag on the fly For example I will get one of the tag in xml is as below <from>Test Test... (12 Replies)
Discussion started by: guddu_12
12 Replies

2. Solaris

XML value compare and replace

I need a way to to check if a value in a file that has this XML format is less than or equal to current system date/time. if it is I need to override it with a future date/time: Here is the data sample: <?xml version="1.0"... (1 Reply)
Discussion started by: mrn6430
1 Replies

3. Shell Programming and Scripting

Need to replace XML TAG

As per the requirement I need to replace XML tag with old to new on one of the XML file. Old<com : DEM>PHI</com : DEM> New<com : DEM>PHM</com : DEM> Please someone provide the sed command to replace above mentioned old XML tag with new XML tag (2 Replies)
Discussion started by: siva83
2 Replies

4. Shell Programming and Scripting

Find and replace from an XML

Input-xml <weblogic-web-app> <session-descriptor> <session-param> <param-name>SysName</param-name> <param-value>smilyface</param-value> </session-param> <session-param> <param-name>InternetProtocol</param-name> <param-value>xxxxxxxx</param-value> ... (2 Replies)
Discussion started by: linuxadmin
2 Replies

5. Linux

Linux command to find and replace occurance of more than two equal sign with "==" from XML file.

Please help me, wasted hrs:wall:, to find this soulution:- I need a command that will work on file (xml) and replace multiple occurrence (more than 2 times) Examples 1. '===' 2. '====' 3. '=======' should be replaced by just '==' Note :- single character should be replaced. (=... (13 Replies)
Discussion started by: RedRocks!!
13 Replies

6. Shell Programming and Scripting

Find Node and replace line(s) preceding in xml file

Hello, I have an xml file whose contacts are like below: <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Mango <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Bannana (3 Replies)
Discussion started by: umarsatti
3 Replies

7. Shell Programming and Scripting

Find/Replace in XML file

I am not sure how to approach this find/replace using a shell script. Any help or guidance appreciated. I want to find this: <objects/> <thumb>thumb_0001.jpg</thumb> <preview>preview_0001.jpg</preview> And replace with something like this: <objects>... (1 Reply)
Discussion started by: jimraynor
1 Replies

8. UNIX for Dummies Questions & Answers

replace %20 in xml file

Hi, I'd like to use sed in order to replace %20 and other "special" characters that are represented with % and some number combination in xml file. Typical line looks like this: /Users/imac1/Music/iTunes/iTunes... (6 Replies)
Discussion started by: andrejm
6 Replies

9. Shell Programming and Scripting

Help with find and replace in XML

Hello Guys, I have this requirement with several hundred files. I have this first set of xml's files with the following tags spread across the file FILE in SET A <Name>Lion</Name> <Age>15</Age> ..... .... ... <Date>2009-12-12</Date> Now i have this another set of files which... (5 Replies)
Discussion started by: aixjadoo
5 Replies

10. UNIX for Dummies Questions & Answers

find and replace in XML

Hi I need one clarication.. I have an xml having many entries like this.. <Cust_Name>Tom Cruise</Cust_Name> I want to rename this to <Cust_Name>TEST</Cust_Name> Pls let me know how to do it.. I was trying some basic commands like grep 'Cust_Name' * | tr '>' ',' | tr '<' ... (2 Replies)
Discussion started by: wip_vasikaran
2 Replies
Login or Register to Ask a Question