Bash shell编程-从多个文件中计算单词
在shell编程方面需要一些帮助 我需要编写一个shell脚本,它接受多个文本文件作为参数,并计算所有文本文件中出现的单词数 例如,file1.txt包含文本Bash shell编程-从多个文件中计算单词,bash,shell,Bash,Shell,在shell编程方面需要一些帮助 我需要编写一个shell脚本,它接受多个文本文件作为参数,并计算所有文本文件中出现的单词数 例如,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
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
.
.
.
提前谢谢怎么样
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
它是根据一个脚本改编而成的,该脚本解释了,然后制作了一个图表:
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 ####################
输出:
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
看看answerHi…我尝试过类似这样的-cat$@|tr-s''\n'|uniq-c | sort-nr,但它没有结合values@user3809572只需在uniq之前排序:
cat$@|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