zoukankan      html  css  js  c++  java
  • awk词频统计

    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 "[[:alpha:]]+" a.log 
    
    awk '{++count[$0]} END{for (word in count){ printf("%-20s%d
    ",word,count[word]);}}'
    
    sort -n -r -k2,2
    

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

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

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

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

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

    root@standby [13:39:48]$ egrep -o "[[:alpha:]]+" a.log |awk '{++count[$0]} END{for (word in count){ printf("%-20s%d
    ",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
    ",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
    ",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

    作者:Standby一生热爱名山大川、草原沙漠,还有妹子
    出处:http://www.cnblogs.com/standby/

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    grafan+cadvisor+prometheus监控docker
    容器化tomcat9.0
    Vue的四种特殊attribute:is key ref v-slot (更新中)
    properties 和 attributes的区别
    vue父子组件(更新中)
    发布者订阅者模式(vue双向绑定原理)
    开发中一些好的逻辑;
    vue项目根据不同环境调用不同请求地址(2)
    linux上删除文件名乱码的文件
    Oracle的SQL优化
  • 原文地址:https://www.cnblogs.com/standby/p/8309994.html
Copyright © 2011-2022 走看看