awk词频统计

本文介绍了两种使用egrep和awk进行文本文件词频统计的方法。第一种方法利用egrep拆分文本并结合awk进行单词级别的统计,第二种方法采用awk直接进行字段统计。这两种方法均能有效地统计出文本中各单词出现的频率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2018-01-03@中关村

 

有文本 a.log 如下,请做词频统计,统计出每个单词出现的频率并倒序排序。

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

 

方法一

egrep -o "\b[[:alpha:]]+\b" a.log 

awk '{++count[$0]} END{for (word in count){ printf("%-20s%d\n",word,count[word]);}}'

sort -n -r -k2,2

- 首先通过egrep把文本内容拆成每行列出一个单词

  - egrep -o 表示只打印匹配到的字符,由换行符分割

  - \b 是正则表达式里的单词边界符

  - [:alpha:] 是表示字母的字符类

- 其次通过awk统计每个单词出现的次数

root@standby [13:39:48]$ egrep -o "\b[[:alpha:]]+\b" a.log |awk '{++count[$0]} END{for (word in count){ printf("%-20s%d\n",word,count[word]);}}' |sort -n -r -k2,2 |head -20
is                  10
than                8
better              8
to                  5
the                 5
one                 3
of                  3
never               3
it                  3
idea                3
be                  3
Although            3
way                 2
should              2
s                   2
obvious             2
may                 2
implementation      2
If                  2
explain             2
root@standby [13:42:38]$ 

  

 方法二

awk '{for(i=1;i<=NF;i++) count[$i]++} END{ for(patten in count) printf("%-20s%d\n",patten,count[patten])}

注意:这种情况统计的就不是单词,而是按照字段统计的

root@standby [15:45:06]$ awk '{for(i=1;i<=NF;i++) count[$i]++} END{ for(patten in count) printf("%-20s%d\n",patten,count[patten])}' a.log |sort -n -r -k2,2 |head -20
is                  10
than                8
better              8
to                  5
the                 5
of                  3
be                  3
Although            3
way                 2
should              2
one                 2
never               2
may                 2
implementation      2
If                  2
idea.               2
explain,            2
do                  2
a                   2
Zen                 1
root@standby [15:45:14]$ 

 

 

参考:https://www.cnblogs.com/Peter2014/p/7596128.html

参考:http://bbs.chinaunix.net/thread-4102008-1-1.html

 

转载于:https://www.cnblogs.com/standby/p/8309994.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值