Sanjay malhotra
Sanjay malhotra

Reputation: 71

How to read & count XML records from a file in UNIX shell Script

I have records inside the XML tags, and I want to get the count of them. In below, e.g. the contents inside the <record> </record> tag should be counted as 1. So for the example below, the count should be 2:

<record>
    hi
    hello
</record>

<record>
    follow
</record>

Could somebody help me with the Unix Shell Script?

Upvotes: 5

Views: 5516

Answers (3)

Ell
Ell

Reputation: 947

grep -c "</record>" file.xml

Upvotes: 3

Raju
Raju

Reputation: 2962

This will work even if the file content is in single line(not in pretty XML format).

perl -nle "print s/<record>//g" < filename | awk '{total += $1} END {print total}'

Upvotes: 4

Jeff
Jeff

Reputation: 9694

Assuming your XML is in a file named file.xml, your solution would be

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

Upvotes: 6

Related Questions