linux shell-实现分析一个文本文件中单词出现的频率

wf.sh

#!/bin/bash

ARGS=1
E_BADARGS=65
E_NOFILE=66

if [ $# -ne "$ARGS" ];then
  echo "Usage:`basename $0` filename"
  exit $E_BADARGS
fi

if [ ! -f "$1" ];then
  echo "File \"$1\" does not exist."
  exit $E_NOFILE
fi

sed -e 's/\,/ /g' -e 's/ /\n/g' $1  | tr A-Z a-z | sort | uniq -c | sort -nr

exit 0

验证:

[root@patrolagent ~]# cat 1.txt 
aaa   bbb
ddd ccc
[root@patrolagent ~]# sh wf.sh 
Usage:wf.sh filename
[root@patrolagent ~]# sh wf.sh aa
File "aa" does not exist.
[root@patrolagent ~]# sh wf.sh 1.txt 
      2 
      1 ddd
      1 ccc
      1 bbb
      1 aaa
[root@patrolagent ~]# 

猜你喜欢

转载自blog.csdn.net/z19861216/article/details/142453821