文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>一个简单的文本查询程序------bash sample(2)

一个简单的文本查询程序------bash sample(2)

时间:2010-08-25  来源:小妮子8308

功能:

1. 检查参数个数;

2. 检查input file是否存在;

3. 列出input file中出现频率最高的10个单词;

4. 列出这10个单词所在的行数和句子内容;

5. 手动输入input string, 在input file中查询string并记录。


  1. #! /bin/bash   
  2. #  
  3. #run:./textsh.sh inputfilename  
  4. ARGNO=1 #expecting number of parameter  
  5. E_WRONGARGS=65  
  6. temp="tempfile"  
  7. output="freout.txt"  
  8. # clean   
  9. rm $output  
  10. # check the parameter  
  11. #if [ $# != $ARGNO ]  
  12. if [ $# -ne $ARGNO ]  
  13. then   
  14.    echo "Parameter error. Expecting only $ARGNO parameter(s)."  
  15.    exit $E_WRONGARGS  
  16. fi  
  17.      
  18. # check file exists  
  19. filename=$1 #get input file name  
  20. if [ -e $filename ]; then  
  21.    echo "Input file is $filename."  
  22. else  
  23.    echo "File $filename could not be found. Please check!"  
  24.    exit $E_WRONGARGS  
  25. fi  
  26. # list 10 most frequent words  
  27. #echo "(1) Here are 10 most frequent words in file $filename:" > $output  
  28. printf "(1) Here are 10 most frequent words in file $filename:\n" > $output  
  29. cat $filename | sed -e "s/[^a-zA-Z]/\n/g" | grep -v ^$ | sort | uniq -c | sort -n -k 1 -r | head -10 > $temp   
  30. cat $temp >> $output  
  31. printf "\n(2) Lists of words:\n" >> $output  
  32. for x in $(awk '{print $NF}' $temp)  
  33. do  
  34.  #  echo -e >> $output  
  35.    printf "\n[$x]:\n" >> $output  
  36.    grep -En "\b$x\b" $1 >> $output  
  37. done   
  38. # manual search  
  39. printf "\n(3) To search what you want?\n" >> $output  
  40. echo "To search what you want? Please input your word!"  
  41. read input  
  42. while [ "$input" != "exit" ]  
  43. do   
  44.    if [ "$input" != "exit" ]  
  45.    then  
  46.       printf "\nsearch for [$input]:\n" >> $output  
  47.       grep -En "\b$input\b" $1 | tee -a $output   
  48.       echo -e | tee -a $output  
  49.       echo "Continue? Please input your word. (To stop by using 'exit'.)"  
  50.       read input       
  51.    fi  
  52. done  
  53. echo -e   
  54. echo "Exit of searching."  
  55. #clean  
  56. rm $temp 

总结:(空了来写)

1. 学习command?

2. 参考?


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载