shell脚本统计文件中单词的个数

一、方案

方法一:

(1)cat file|sed 's/[,.:;/!?]/ /g'|awk '{for(i=1;i<=NF;i++)array[$i]++;}END{for(i in array) print i,array[i]}'   #其中file为要操作的文件,sed中/ /间有一个空格。

(2)sed 's/[,.:;/!?]/ /g' file|awk '{for(i=1;i<=NF;i++)array[$i]++;}END{for(i in array) print i,array[i]}'     #(1)和(2)效果一致。

方法二:

(1)awk 'BEGIN{RS="[,.:;/!?]"}{for(i=1;i<=NF;i++)array[$i]++;}END{for(i in array) print i,array[i]}' file

二、验证

[root@hehe668 shell]# cat file
hello world,hi girl;how old are you?
where are you from?
how are you?
i am fine!thinks.
and you?
http://www.cnblogs.com/youxuguang/

[root@hehe668 shell]# cat file|sed 's/[,.:;/!?]/ /g'|awk '{for(i=1;i<=NF;i++)array[$i]++;}END{for(i in array) print i,array[i]}'
com 1
http 1
from 1
www 1
i 1
you 4
hi 1
hello 1
youxuguang 1
and 1
world 1
cnblogs 1
where 1
old 1
how 2
fine 1
am 1
are 3
girl 1
thinks 1

[root@hehe668 shell]# sed 's/[,.:;/!?]/ /g' file|awk '{for(i=1;i<=NF;i++)array[$i]++;}END{for(i in array) print i,array[i]}'
com 1
http 1
from 1
www 1
i 1
you 4
hi 1
hello 1
youxuguang 1
and 1
world 1
cnblogs 1
where 1
old 1
how 2
fine 1
am 1
are 3
girl 1
thinks 1

[root@hehe668 shell]# awk 'BEGIN{RS="[,.:;/!?]"}{for(i=1;i<=NF;i++)array[$i]++;}END{for(i in array) print i,array[i]}' file
com 1
http 1
from 1
www 1
i 1
you 4
hi 1
hello 1
youxuguang 1
and 1
world 1
cnblogs 1
where 1
old 1
how 2
fine 1
am 1
are 3
girl 1
thinks 1

原文:http://www.cnblogs.com/youxuguang/p/5917215.html

posted @ 2016-09-28 17:19  hehe668  阅读(5709)  评论(0编辑  收藏  举报