Counting number of character occurrences per line
<p>I have a file that looks like this: ( Note : A*, B*, C* are placeholders). The file is delimited by <code>;</code></p>

<pre><code>AAAA;BBBB;CCCCCCCC;DD;EEEEEEEE;FF;
AAA1;BBBBB;CCCC;DD;EEEEEEEE;FFFFF;
AAA3;BB;CCCC;DDDDDDDDD;EEEEEEE;FF;
</code></pre>

<p>I m trying to write a small script that counts the number of occurrences of the delimiter <code>;</code> and if it is lesser or greater than 5, output said line to a text file. </p>

<pre><code>delim=";"

while read line
do  
    n_of_occ=$(grep -o "$delim" &lt;&lt;&lt; "$line" | wc -l)

    if [[ $n_of_occ &lt; 5 ]] || [[ $n_of_occ &gt; 5 ]]
    then
        echo $line &gt;&gt; outfile
    fi
done
</code></pre>

<p>For some reason, this doesn't seem to work and my output is garbled. Could someone assist or provide a different way to tackle this? Perhaps with Perl instead of bash? </p>