• 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。

  • 相关阅读:
    CCF真题之Z字形扫描
    CCF真题之门禁系统
    A
    安装 Spring 框架库
    安装 Apache Commons Logging API步骤
    Manven下载
    669. 修剪二叉搜索树
    UnixLinux | 总结笔记 |文件系统
    561. 数组拆分 I
    620. 有趣的电影
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/5348645.html
Copyright © 2020-2023  润新知