title: BASH: Count identical lines
body: <p>I have a file that contains:</p>
  
  <pre><code>VoicemailButtonTest
  VoicemailButtonTest
  VoicemailButtonTest
  VoicemailButtonTest
  VoicemailButtonTest
  VoiceMailConfig60CharsTest
  VoicemailDefaultTypeTest
  VoiceMailIconSelectableTest
  VoiceMailIconSelectableTest
  VoiceMailIconSelectableTest
  VoiceMailIconSelectableTest
  VoiceMailIconSelectableTest
  VoicemailSettingsFromMessageModeScreenTest
  VoicemailSettingsFromMessageModeScreenTest
  VoicemailSettingsTest
  VoicemailSettingsTest
  VoicemailSettingsTest
  VoicemailSettingsTest
  VoicemailSettingsTest
  VoicemailSettingsTest
  VoicemailSettingsTest
  </code></pre>
  
  <p>How do I replace the duplicate lines with counts:</p>
  
  <pre><code>VoicemailButtonTest (5)
  VoiceMailConfig60CharsTest (1)
  VoicemailDefaultTypeTest (1)
  VoiceMailIconSelectableTest (5)
  VoicemailSettingsFromMessageModeScreenTest (2)
  VoicemailSettingsTest (7)
  </code></pre>
  
  <p>I placing the pair into an associative array. I tried using 'read' inside a 'while' statement, but the array gets lost. Here's my attempt:</p>
  
  <pre><code>unset line
  tests=$(cat file.log)
  echo "$tests" | 
      while read l; do 
          if [ "$l" == "${line}" ]; then
              let cnt++;
          else
              echo "${line} (${cnt})"
              line=${l}
              cnt=1
          fi
          export run_suites
      done
  </code></pre>