Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count number of xml element from linux shell

My xml looks something like this :

<elements>
<elem>
....bunch of other elements
</elem>
</elements>

Is there a way to count the number of occurances of elem tag in some xml file trough linux shell? like with perl/python or anything that might work as one liner?

I might try something like grep -c "elem" myfile.xml and the number I get divide it by 2 and get the number, is there something similar but one liner?

EDIT :

I'm looking for alternative grep solution

like image 504
London Avatar asked Apr 26 '11 12:04

London


4 Answers

You can also use xmllint:

xmllint --xpath "count(//elem)" myfile.xml
like image 138
bluenote10 Avatar answered Nov 19 '22 23:11

bluenote10


The xml_grep tool does what you want - try the following:

xml_grep --count //elem example.xml

That utility is in the xml-twig-tools package on Debian / Ubuntu, and the documentation is here.

like image 37
Mark Longair Avatar answered Nov 19 '22 23:11

Mark Longair


DO NOT USE REGULAR EXPRESSIONS TO PARSE OR SCAN XML FILES

The mandatory disclaimer being fired, here's my solution:

xmllint --nocdata --format myfile.xml | grep -c '</elem>'

xmllint is part of libxml which is fairly common on many linux distros. This solution passes the following regex/XML traps:

  • spurious spaces (--format)
  • several closing tags on single line (--format)
  • CDATA sections (--nocdata)

However, you will be caught by nasty namespace declaration and defaults.

like image 3
Robert Bossy Avatar answered Nov 19 '22 23:11

Robert Bossy


London,

Try fgrep -c '</elem>' $filename

fgrep is a standard unix utility, not at all sure about linux though. The -c switch means count.

Cheers. Keith.

PS: It's allmost allways easier to count CLOSING tags, coz they don't have attributes ;-)

like image 1
corlettk Avatar answered Nov 19 '22 23:11

corlettk