一、方案
方法一:
(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 1http 1from 1www 1i 1you 4hi 1hello 1youxuguang 1and 1world 1cnblogs 1where 1old 1how 2fine 1am 1are 3girl 1thinks 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 1http 1from 1www 1i 1you 4hi 1hello 1youxuguang 1and 1world 1cnblogs 1where 1old 1how 2fine 1am 1are 3girl 1thinks 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 1http 1from 1www 1i 1you 4hi 1hello 1youxuguang 1and 1world 1cnblogs 1where 1old 1how 2fine 1am 1are 3girl 1thinks 1原文: