脚本-统计词频

#!/bin/bash
# file name     : word_freq.sh
# usage         : 统计词频

if [ $# -ne 1 ] ;then
 echo "usage: $0 filename"
 exit 1
fi

filename=$1
#egrep 使用扩展的正则,-o只输出匹配到的文本
egrep -o "\b[[:alpha:]]+\b" $filename | \
#count为关联数组 
 awk '{ count[$0]++  } 
 END { 
   printf("%-14s%s\n","Word","Count");
   for(ind in count){
    printf("%-14s%d\n",ind,count[ind]);
   }
 }'


[root@localhost practice-sh]# ./word_freq.sh t4.txt 
Word          Count
i             4
dog           1
docker        1
student       1
teacher       1
a             4
am            4

 

你可能感兴趣的:(Linux,笔记,脚本)