This sounds like a variation on this questionthis question.
So I'll work on that answer:
for w in `cat test.tex`; do echo $w; done|sort|egrep '\\[a-z]*{'|uniq -c 1 A}&\multicolumn{2}{c|}{Cavity 1 \begin{figure}[H] 1 \begin{minipage}[b]{.46\textwidth} 1 \begin{tabular}{|c|cS|cS|} 1 \begin{tikzpicture}[remember The regular expression may need some work but this should get you started.