Extract timestamp from first record in xml file and it checks if not it will replace first record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract timestamp from first record in xml file and it checks if not it will replace first record
# 1  
Old 11-05-2014
Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml
Code:
<emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> 
<Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> 
<Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> 
<Join><id>101</id><city>sydney</city><date>06/06/14 0623</date></join> 
<emp><id>102</id><name>BBB</name><date>09/09/14 2001</date></emp> 
<Join><id>102</id><city>new york</city><date>09/09/14 1410</date></join> 
<Join><id>102</id><city>perth</city><date>09/08/14 2001</date></join> 
<Join><id>102</id><city>tulsa</city><date>09/09/14 1919</date></join>

timestamp format : MM/DD/YY HHMM

for example,

Extract 'emp' timestamp for first row (06/06/14 1811) and checks 'join' timestamp rows. if not less equal to another timestamp then replace from 'emp' timestamp into 'join' row

My output.xml should be as,

Code:
 <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> 
 <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> 
 <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> 
 <Join><id>101</id><city>sydney</city><date>06/06/14 1811</date></join> 
 <emp><id>102</id><name>BBB</name><date>09/09/14 2001</date></emp> 
 <Join><id>102</id><city>new york</city><date>09/09/14 2001</date></join> 
 <Join><id>102</id><city>perth</city><date>09/09/14 2001</date></join> 
 <Join><id>102</id><city>tulsa</city><date>09/09/14 2001</date></join>

this is example I have huge xml file

this is my code

Code:
 for i in `cat test.xml` 
 do 
    if [[ "$i" == "<emp>"* ]]  ; then 
    empvar=`echo $i | grep -o -P '(?<=<date>).*(?=</date>)' ` 
    empdate=`date --date="$empvar" +%s` 
    echo $i >> ouput.xml 
    else 
    joinvar=`echo $i | grep -o -P '(?<=<date>).*(?=</date>)'` 
    joindate=`date --date="$joinvar" +%s` 
             if [[ $empdate -le $joindate ]]; then 
            echo $i >> output.xml 
            else 
            echo $i | sed 's#<date>\([^<][^<]*\)</date>#<date>'$empvar'</date>#' >> output.xml 
            fi 
    fi 
 done

this code is working and it is taking long time to complete so, I need better way to process
# 2  
Old 11-05-2014
Try something like:
Code:
awk -F '</?date>' '
#{printf("%s \"%s\"\n", substr($0, 1, 2), $2)}
/^<emp>/ {
	ed = $2
	cd = substr($2, 7, 2) substr($2, 1, 2) substr($2, 4, 2) substr($2, 10)
	print
	next
}
/^<Join>/ {
	if(cd > (substr($2, 7, 2) substr($2, 1, 2) substr($2, 4, 2) substr($2, 10)))
		$0 = $1 "<date>" ed "</date>" $3
}
1' test.xml

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
# 3  
Old 11-06-2014
Thanks you so much @Don Cragun, it works perfect!!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string for every record after the 1st record

I have data coming in the below format for each record <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox> <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox... (8 Replies)
Discussion started by: dsravanam
8 Replies

2. Shell Programming and Scripting

Extract record from file based on section.

input file output file (1 Reply)
Discussion started by: lathigara
1 Replies

3. UNIX for Dummies Questions & Answers

Delete a record in a xml file using shell scripting

find pattern, delete line with pattern and 3 lines above and 8 lines below the pattern. The pattern is "isup". The entire record with starting tag <record> and ending tag </record> containing the pattern is to be deleted and the rest to be retained. <record> ... (4 Replies)
Discussion started by: sdesstp
4 Replies

4. Shell Programming and Scripting

File Processing - How to Replace particular record

I have a file with data as 1 L 1 Used 1 1 1 1 L ... (6 Replies)
Discussion started by: karumudi7
6 Replies

5. Shell Programming and Scripting

Script to perform record format checks

Hi All, I have a requirement to perform the following checks. Input file is a "|" delimited file and looks like this. A|c1|c2|c3|.... B|G1|G2|G3.... C|H1|H2|H3... A|c4|c5|c6|.... B|G4|G5|G6.... C|H4|H5|H6... Now the check is to see if all the "A" records have a corresponding B... (7 Replies)
Discussion started by: gsjdrr
7 Replies

6. Shell Programming and Scripting

How to extract first and last line of different record from a file

Hi all I want to inquire that is there any unix command that can help me while extracting first and last line in a file ( TEST.dat) for example in the below record i want to extract the line that are in BOLD text or in other words i want to extract line no 1,3,4 and 7 aa 1 2 3 aa 2 3 4... (5 Replies)
Discussion started by: Bungash125
5 Replies

7. UNIX for Advanced & Expert Users

How to read an Xml record contained in a file--urgent

Hi I have an xml file which has multiple xml records.. I don't know how to read those records and pipe them to another shell command the file is like <abc>z<def>y<ghi>x........</ghi></def></abc> (1st record) <jkl>z<mno>y<pqr>x........</pqr></mno></jkl> (2nd record) Each record end... (4 Replies)
Discussion started by: aixjadoo
4 Replies

8. UNIX for Dummies Questions & Answers

how to read record by record from a file in unix

Hi guys, i have a big file with the following format.This includes header(H),detail(D) and trailer(T) information in the file.My problem is i have to search for the character "6h" at 14 th and 15 th position in all the records .if it is there i have to write all those records into a... (1 Reply)
Discussion started by: raoscb
1 Replies

9. Shell Programming and Scripting

splitting a record and adding a record to a file

Hi, I am new to UNIX scripting and woiuld appreicate your help... Input file contains only one (but long) record: aaaaabbbbbcccccddddd..... Desired file: NEW RECORD #new record (hardcoded) added as first record - its length is irrelevant# aaaaa bbbbb ccccc ddddd ... ... ... (1 Reply)
Discussion started by: rsolap
1 Replies

10. Shell Programming and Scripting

remove duplicated xml record in a file under unix

Hi, If i have a file with xml format, i would like to remove duplicated records and save to a new file. Is it possible...to write script to do it? (8 Replies)
Discussion started by: happyv
8 Replies
Login or Register to Ask a Question