shell 统计词频脚本

  

#!/bin/bash
if [ $# -ne 1 ];
then
        echo "Usage:$0 filename";
        exit -1
fi

filename=$1
egrep -o "[[:alpha:]]+" $filename | awk '{count[$0]++}END{printf("%-14s%s
","Word","Count");for(ind in count){printf("%-14s%d
",ind,count[ind]);}}'

  这里注意两点

egrep 和grep的区别:egrep 支持的正则更全一点

The symbol 
matches the empty string at the edge of a word 匹配一个单词边界的空字符串

<  >

The symbols < and > respectively match the empty string at the beginning and end of  a  word. 匹配单词的开头或者结尾空串

%-14s - 表示左对齐 14 表示 字符串宽度为14

[:alpha:] 表示正则匹配 相当于 a-z A-Z 详见:http://www.cnblogs.com/zhuyp1015/archive/2012/07/01/2572289.html 
原文地址:https://www.cnblogs.com/ggbond1988/p/4812527.html