computing
  • 0

Count The No. Of Records In a Xml File + Unix

  • 0

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

Share

1 Answer

  1. 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.

    • 0