count the no.of records in a xml file + unix

January 4, 2011 at 06:38:00
Specs: Windows XP
Hi Gurus,

I have a XML file in unix which contains some 1392 records when I do a wc-l command on the filename,

Say example,

wc -l filename.xml

I would like to know how to do a count of records as xml file and normal file is not same as XML contains the open parent tag and the corresponding closing tag.

So for an effective way of counting the total no. of records of a XML file in unix, what should be done?

I am just learning Unix and would like to have a thought on this..

Thanks,
Bharathih


See More: count the no.of records in a xml file + unix

Report •

#1
January 7, 2011 at 18:54:19
So if the xml tag is <parent>, then why not:

grep "<parent>" file.xml | wc -l

If you want to see how you'd write a script with a counter to do this, then try this:

#!/bin/ksh

Counter=0
Start=0

exec 3<junk.xml.conv

while read -u3 line
do
if [[ $line == *"<parent>" ]] && [[ $Start -eq 0 ]]
then
Start=1

elif [[ $line == *"<parent>" ]] && [[ $Start -eq 1 ]]
then
print "Error, error!!! Must sterilize"
exit 1
elif [[ $line == *"</parent>" ]] && [[ $Start -eq 1 ]]
then
(( Counter += 1 ))
Start=0
print $Start
fi
done

print $Counter

I stuck that error check in there in the event a parent element doesn't have a closing tag.


Report •
Related Solutions


Ask Question