I'd like to count number of xml nodes in my xml file(grep or somehow).
....
<countryCode>GBR</countryCode>
<countryCode>USA</countryCode>
<countryCode>CAN</countryCode>
...
<countryCode>CAN</countryCode>
<someNode>USA</someNode>
<countryCode>CAN</countryCode>
<someNode>Otherone</someNode>
<countryCode>GBR</countryCode>
...
How to get count of individual countries like CAN = 3, USA = 1, GBR = 2? Without passing in the names of the countries there might be some more countries?
Update:
There are other nodes beside countrycode
Quick and simple:
grep countryCode ./file.xml | sort | uniq -c
Dummy is ur file name and replace 6 in -6 with n-2(n - no of lines in ur data file)
quick and dirty (only based on your example text):
test:
My simple suggestion would be to use
sort
anduniq -c
Where you'd pipe in the output of your
grep
instead of anecho
. A more robust solution would be to use XPath. If youre XML file looks likeThen you could use:
I say it's more robust because using tools designed for parsing flat text will be inherently flaky for dealing with XML. Depending on the context of the original XML file, a different XPath query might work better, which would match them anywhere:
Something like this maybe:
Of course you need to provide regex that matches your needs.