Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 0.93 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Count the occurrence of a string in an input file
  2. #!/bin/bash
  3.  
  4. INPUT_FILE="$1"
  5. declare -a LIST_CHARS
  6.  
  7. if [ $# -ne 1 ]
  8. then
  9.         echo "Usage: $0 <file_name>"
  10.         exit 1
  11. fi
  12.  
  13.  
  14. if [ ! -f $INPUT_FILE ]
  15. then
  16.         echo "$INPUT_FILE does not exists. Please specify correct file name"
  17.         exit 2
  18. fi
  19.  
  20. while read line
  21. do
  22.         while read i
  23.         do
  24.                 echo $line
  25.                 count=`grep -i $line | wc -l`
  26.                 echo "String $line appears $count times"
  27.         done < $INPUT_FILE
  28.  
  29. done < $INPUT_FILE
  30.        
  31. &#47;.&#47;
  32.        
  33. sort -f FILE | uniq -ic
  34.        
  35. s/^ *([0-9]+) (.*)/2 appears 1 times/
  36.        
  37. while read line
  38. do  
  39.     uc=$(echo $line | tr [a-z] [A-Z] | tr -d ' ')
  40.     echo  $uc $(grep -i "$uc" strs.txt | wc -l)
  41. done< data.txt | sort | uniq
  42.        
  43. 31
  44. ALLEN 6
  45. MARK 4
  46. MOKADDIM 1
  47. SHIPLU 1
  48. TIM 4
  49.        
  50. sort -f data.txt | uniq -i -c  | while read num word
  51. do  
  52.     echo $(echo $word|tr [a-z] [A-Z])  appeard  $num times
  53. done