title: Count the occurrence of a string in an input file
body: <p>There is a shell script which is supposed to process an incoming text file. </p>
  
  <p>This text file contains strings split on multiple lines, and each string is present more than once.</p>
  
  <p>The shell script needs to read this text file and output the String and count of each string.</p>
  
  <p>Consider the text file is:</p>
  
  <blockquote>
    <p>Tim </p>
    
    <p>tim </p>
    
    <p>Mark </p>
    
    <p>MARk</p>
    
    <p>Allen </p>
    
    <p>ALLen </p>
    
    <p>allEN</p>
  </blockquote>
  
  <p>The output should be like this:</p>
  
  <blockquote>
    <p>Tim appears 2 times</p>
    
    <p>Mark appears 2 times </p>
    
    <p>Allen appears 3 times</p>
  </blockquote>
  
  <p>Right now, I am able to print the occurrence of strings, but that gets repeated the number of times the string occurs, that is "Tim appears 2 times" gets printed twice. I was trying to replace a string with NULL as soon as I count its occurrence, but for some reason, the sed is not working, coz maybe I am not invoking it at the right place (or in right way) </p>
  
  <pre><code> #!/bin/bash
  
  INPUT_FILE="$1"
  declare -a LIST_CHARS
  
  if [ $# -ne 1 ]
  then
          echo "Usage: $0 &lt;file_name&gt;"
          exit 1
  fi
  
  
  if [ ! -f $INPUT_FILE ]
  then
          echo "$INPUT_FILE does not exists. Please specify correct file name"
          exit 2
  fi
  
  while read line
  do
          while read i
          do
                  echo $line
                  count=`grep -i $line | wc -l`
                  echo "String $line appears $count times"
          done &lt; $INPUT_FILE
  
  done &lt; $INPUT_FILE
  </code></pre>