shell 编程 - 从多个文件中计算单词
shell programming - counting words from multiple files
在 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
a 2
white 1
.
.
.
提前致谢
怎么样
cat file*.txt |
xargs -n1 |
awk '{h[]++}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[]++}END{for(i in h){print h[i],i|"sort -rn|head -20"}}' |awk '!max{max=;}{r="";i=s=60*/max;while(i-->0)r=r"#";printf "%15s %5d %s %s",,,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 ####################
#!/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
在 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
a 2
white 1
.
.
.
提前致谢
怎么样
cat file*.txt |
xargs -n1 |
awk '{h[]++}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[]++}END{for(i in h){print h[i],i|"sort -rn|head -20"}}' |awk '!max{max=;}{r="";i=s=60*/max;while(i-->0)r=r"#";printf "%15s %5d %s %s",,,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 ####################
#!/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