shell编程 - 计算多个文件中的单词

时间:2015-01-20 22:26:17

标签: bash shell

在shell编程方面需要一些帮助。

我需要编写一个shell脚本,它接受多个文本文件作为参数,并计算所有这些单词的出现次数。

对于Eg file1.txt包含文本

mary had a little lamb. His fleece was white as a snow. And everywhere that mary went.

和file2.txt包含

Mary had a little lamb. Hello How are you

所以脚本应该输出像

这样的输出
Mary 2
Had 2
a  2
white 1
.
.
.

提前致谢

2 个答案:

答案 0 :(得分:0)

怎么样

cat file*.txt | 
    xargs -n1 |
    awk '{h[$1]++}END{for(i in h){print h[i],i|"sort -rn|head -20"}}' 

打印

3 a
2 mary
2 little
2 lamb.
2 had
1 you
1 white
1 went.
1 was
1 that
1 snow.
1 Mary
1 How
1 His
1 Hello
1 fleece
1 everywhere
1 as
1 are
1 And

它是根据解释 on this website 的脚本改编而来的,它继续绘制图表:

cat file*.txt | xargs -n1 | awk '{h[$1]++}END{for(i in h){print h[i],i|"sort -rn|head -20"}}' |awk '!max{max=$1;}{r="";i=s=60*$1/max;while(i-->0)r=r"#";printf "%15s %5d %s %s",$2,$1,r,"\n";}'

打印

          a     3 ############################################################ 
       mary     2 ######################################## 
     little     2 ######################################## 
      lamb.     2 ######################################## 
        had     2 ######################################## 
        you     1 #################### 
      white     1 #################### 
      went.     1 #################### 
        was     1 #################### 
       that     1 #################### 
      snow.     1 #################### 
       Mary     1 #################### 
        How     1 #################### 
        His     1 #################### 
      Hello     1 #################### 
     fleece     1 #################### 
 everywhere     1 #################### 
         as     1 #################### 
        are     1 #################### 
        And     1 #################### 

答案 1 :(得分:0)

#!/bin/sh

str=""
for i in $@
do
    str="${str}$(sed 's|\.||g' $i) " # remove the period and add space between files.
done
echo $str | tr -s ' ' '\n' | sort | uniq -c | sort -nr

$ thescript file1.txt file2.txt

输出:

  3 a
  2 mary
  2 little
  2 lamb
  2 had
  1 you
  1 white
  1 went
  1 was
  1 that
  1 snow
  1 Mary
  1 How
  1 His
  1 Hello
  1 fleece
  1 everywhere
  1 as
  1 are
  1 And