Chinaunix首页 | 论坛 | 博客
  • 博客访问: 457352
  • 博文数量: 148
  • 博客积分: 4424
  • 博客等级: 上校
  • 技术积分: 1211
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-25 21:50
文章分类

全部博文(148)

文章存档

2012年(89)

2011年(20)

2010年(39)

分类: LINUX

2010-08-25 22:18:38

功能:

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. 参考?


阅读(531) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-08-28 08:48:01

Download More than 1000 free IT eBooks: http://free-ebooks.appspot.com