Shell Script syntax for XML processing

Hi All,

I am new to Shell scripting.

I have a log file containing XML Messages.Each XML Message is accompanied with a timestamp.I need to count the the number of messages that get logged in a particular timeinterval.Is there any command/Syntax to achieve this. 

Any code/example is welcome.

Thanks
Vignesh.

:wink:

I thought it should have been the other way.

To parse XML messages, I suggest using XML::Parser

XML::Parser - A perl module for parsing XML documents - search.cpan.org

Quite easy, quick and efficient to use the above module

Hi madhan,

Thanks for ur reply.Actually there is no need to parse my XML.Each XML message is accompanied with a timestamp value(it is not a XML field).I just need to count the no of messages within a particular time interval using shell scripting.

My XML looks like this

2008-01-27 00:05:00 (2008-01-27 00:05:00.055000000Z): message={Data="<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
....
....
</Header>
<Body>
....
....
</Body>
</Envelope>"
2008-01-27 00:05:12 (2008-01-27 00:05:12.055000000Z): message={Data="<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
....
....
</Header>
<Body>
....
....
</Body>
</Envelope>"

Thanks
Vignesh

Can be done in python (works upto seconds (not milliseconds) and also works across days):

LOG

2008-01-27 00:05:00 (2008-01-27 00:05:00.055000000Z): message={Data="<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
....
....
</Header>
<Body>
....
....
</Body>
</Envelope>
2008-01-27 00:05:12 (2008-01-27 00:05:12.055000000Z): message={Data="<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
....
....
</Header>
<Body>
....
....
</Body>
</Envelope>
2008-01-27 00:05:12 (2008-01-27 00:06:10.055000000Z): message={Data="<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
....
....
</Header>
<Body>
....
....
</Body>
</Envelope>

Python script:

import time
import datetime
import re
import sys

#---------------------------------------------------------
# Variables and defs
#---------------------------------------------------------

if len(sys.argv) == 1:
        print "Usage : ", sys.argv[0], "<file_name>", "from_date(in_double_quotes)", "to_date(in_double_quotes)"
        quit()

[file, frm, to] = sys.argv[1:4]
dtfmt = "%Y-%m-%d %H:%M:%S"
patstr = "^[0-9]{4}-[0-9]{2}-[0-9]{2} "

def mk_date(dstr, dpat):
        return time.mktime(time.strptime(dstr, dpat))

fobj = mk_date(frm, dtfmt)
tobj = mk_date(to, dtfmt)
datepat = re.compile(patstr)

#---------------------------------------------------------
# main part
#---------------------------------------------------------
fh = open(file, 'r')
count = 0
for line in fh:
        if (datepat.match(line)):
                dt = line.split('(')[1].split(')')[0][0:19]
                dobj = mk_date(dt, dtfmt)
                if tobj > dobj > fobj:
                        count += 1
print "Total ocurrances between ", frm, " and ", to, ":", count

fh.close()

Usage:

C:\>lr.py
Usage :  C:\lr.py <file_name> from_date(in_double_quotes) to_date(in_double_quotes)

C:\>lr.py log.xml "2008-01-27 00:04:59" "2008-01-27 00:05:13"
Total ocurrances between  2008-01-27 00:04:59  and  2008-01-27 00:05:13 : 2

HTH

Thanks for your help.

A perl solution:

#!/usr/bin/perl
use strict;
use warnings;
my ($start,$end) = @ARGV;
unless ($start =~ /^\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d$/ && 
        $end   =~ /^\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d$/) {
   die qq{Usage: perl path/to/count.pl "start" "end"
start and end = "YYYY-MM-DD HH:MM:SS" including quotes};
}
my $count = 0;
(my $s = $start) =~ tr/- ://d;
(my $e = $end)   =~ tr/- ://d;
open (my $in, 'path/to/input_file') or die "$!"; 
while(<$in>){
    if (/^(\d\d\d\d)-(\d\d)-(\d\d)\s+(\d\d):(\d\d):(\d\d)/) {
        $count++ if ($e ge "$1$2$3$4$5$6" && "$1$2$3$4$5$6" ge $s);
    }
}
close $in;
print "Number of messages found between $start and $end: $count\n";