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
You can also use xmllint
:
xmllint --xpath "count(//elem)" myfile.xml
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.
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:
However, you will be caught by nasty namespace declaration and defaults.
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 ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With