1. Use XML-specific utilities
I think that any command line tool designed to work with XML is better than custom awk/sed hacks. Scripts using such tools are more robust and do not break when the XML input is slightly reformatted (e.g. it doesn't matter where line breaks are and how the document is indented). My tool of choice for XML querying from the command line is xmlstarlet.
2. Fix your XML
Then, you need to fix your XML: close tags properly and add a root element. Something like this:
<root>
<CONTEXT_1>aaaa</CONTEXT_1>
<CONTEXT_2>bb</CONTEXT_2>
<CONTEXT_2>dfgh</CONTEXT_2>
<CONTEXT_6>bb</CONTEXT_6>
<CONTEXT_1>bbbb</CONTEXT_1>
</root>
3. Use XPath and XSLT
Select the elements you need with XPath and process them with XSLT expressions. In your example, you can count elements' length with
$ xmlstarlet sel -t -m '//root/*' -v "name(.)" -o ": " -v "string-length(.)" -n test.xml
//root/*
selects all child nodes of the root
. name(.)
prints element name of the currently selected element, and string-length(.)
prints the length of its contents.
And get the output:
CONTEXT_1: 4
CONTEXT_2: 2
CONTEXT_2: 4
CONTEXT_6: 2
CONTEXT_1: 4
Group results as you like with awk
or similar tools.