5

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?

2
  • You are not going to include a sample of your data ?
    – Jeff
    Mar 24, 2016 at 14:05
  • <error_records> <record record_no = "1" error_code="100">&apos;NULL&apos;;&apos;C20557&apos;;&apos;090&apos;;&apos;NULL&apos; </record> <record record_no = "5" error_code="101">&apos;NULL&apos;;&apos;20557&apos;;&apos;090&apos;;&apos;NULL&apos; </record> </error_records> here the count should be two. Mar 24, 2016 at 14:23

3 Answers 3

6

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

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

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}'
3
grep -c "</record>" file.xml
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.