• Shell练习 统计单词个数,降序排列


    原文:https://leetcode.com/problems/word-frequency/
    Write a bash script to calculate the frequency of each word in a text file words.txt.
    
    For simplicity sake, you may assume:
    
    words.txt contains only lowercase characters and space ' ' characters.
    Each word must consist of lowercase characters only.
    Words are separated by one or more whitespace characters.
    For example, assume that words.txt has the following content:
    
    the day is sunny the the
    the sunny is is
    Your script should output the following, sorted by descending frequency:
    the 4
    is 3
    sunny 2
    day 1

    简单点就是说给你一个文件,里面是又空格【可以有多个】分割好的单词【都是小写】,请统计一下不同单词的个数,并降序输出。

    将单词记录到数组中,之后遍历输出,使用sort排序,指定第2列按数字降序,即可。

    答案:

    awk '{for(i=1;i<=NF;i++) num[$i]++;} END{ for(k in num) print k" "num[k]}'  words.txt|sort -rnk 2

    使用sort排序,指定哪一列使用-k,后面跟数字,按数字进行排序,使用-n,降序排列使用-r。

  • 相关阅读:
    OC实现个人中心页面
    3D Touch开发技巧的笔记
    APP快速搭建框架
    Swift10大开源项目记录
    iOS进阶学习笔记
    如何使启动界面图片全屏
    【DRF框架】restfull规范
    【DRF框架】版本控制组件
    【DRF框架】路由组件
    【DRF框架】视图组件
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/5348645.html
Copyright © 2020-2023  润新知