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

Note:
Don't worry about handling ties, it is guaranteed that each word's frequency count is unique.

[show hint]

Hint:
Could you write it in one-line using Unix pipes?

这道题给了我们一个文本文件,让我们统计里面单词出现的个数,提示中让我们用管道Pipes来做,在之前那道Tenth Line中,我们使用过管道命令,管道命令的讲解请参见这个帖子。提示中让我们用管道连接各种命令,然后一行搞定,那么我们先来看第一种解法,乍一看啥都不明白,咋办?没关系,容我慢慢来讲解。首先用的关键字是grep命令,该命令一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来,详解请参见这个帖子。后面紧跟的-oE '[a-z]+'参数表示原文本内容变成一个单词一行的存储方式,于是此时文本的内容就变成了:

the
day
is
sunny
the
the
the
sunny
is

下面的sort命令就是用来排序的,参见这个帖子。排完序的结果为:

day
is
is
is
sunny
sunny
the
the
the
the

后面的uniq命令是表示去除重复行命令(参见这个帖子),后面的参数-c表示在每行前加上表示相应行目出现次数的前缀编号,得到结果如下:

1 day
   3 is
   2 sunny
   4 the

然后我们再sort一下,后面的参数-nr表示按数值进行降序排列,得到结果:

4 the
   3 is
   2 sunny
   1 day

而最后的awk命令就是将结果输出,两列颠倒位置即可:

解法一:

grep -oE '[a-z]+' words.txt | sort | uniq -c | sort -nr | awk '{print $2" "$1}' 

下面这种方法用的关键字是tr命令,该命令主要用来进行字符替换或者大小写替换,详解请参见这个帖子。后面紧跟的-s参数表示如果发现连续的字符,就把它们缩减为1个,而后面的‘ ’和‘\n'就是空格和回车,意思是把所有的空格都换成回车,那么第一段命令tr -s ' ' '\n' < words.txt 就好理解了,将单词之间的空格都换成回车,跟上面的第一段实现的作用相同,后面就完全一样了,参见上面的讲解:

解法二:

tr -s ' ' '\n' < words.txt | sort | uniq -c | sort -nr | awk '{print $2, $1}'

下面这种方法就没有用管道命令进行一行写法了,而是使用了强大的文本分析工具awk进行写类C的代码来实现,这种写法在之前的那道Transpose File已经讲解过了,这里就不多说了,最后要注意的是sort命令的参数-nr -k 2表示按第二列的降序数值排列:

解法三:

awk '{
for (i = ; i <= NF; ++i) ++s[$i];
} END {
for (i in s) print i, s[i];
}' words.txt | sort -nr -k 2

参考资料:

https://leetcode.com/discuss/33353/my-accepted-answer-using-tr-sort-uniq-and-awk

https://leetcode.com/discuss/46976/my-ac-solution-one-pipe-command-but-cost-20ms

https://leetcode.com/discuss/29001/solution-using-awk-and-pipes-with-explaination

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Word Frequency 单词频率的更多相关文章

  1. [LeetCode] Word Squares 单词平方

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  2. [LeetCode] Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

  3. [Leetcode] word search 单词查询

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  4. [Leetcode] word ladder 单词阶梯

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  5. LeetCode(192. Word Frequency)

    192. Word Frequency Write a bash script to calculate the frequency of each word in a text file words ...

  6. [Bash]LeetCode192. 统计词频 | Word Frequency

    Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...

  7. Word Frequency

    https://leetcode.com/problems/word-frequency/ Write a bash script to calculate the frequency of each ...

  8. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  9. 统计单词频率--map

    问题描述: 输入一个单词列表,每行一个单词,统计单词出现的频率 思路: 主要是使用c++中的map容器.map实质上是一个二叉查找树,可以做到插入.删除.查询,平均查询时间在O(logn).n为map ...

随机推荐

  1. OSGi 基本原理

    定义 OSGi(Open Service Gateway Initiative)技术是面向Java的动态模型系统. 这个框架实现了一个优雅.完整和动态地组价模型.应用程序(称为bundle)无序重新引 ...

  2. 设计模式(十)组合模式(Composite Pattern)

    一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...

  3. 发布在即!.NET Core 1.0 RC2已准备就绪!!

    先说点废话,从去年夏天就开始关注学习ASP.NET Core,那时候的版本还是beta5,断断续续不停踩坑.一路研究到11月份RC1发布. 在这个乐此不疲的过程里,学习了很多新的东西,对ASP.NET ...

  4. HTML课上小结

    HTML翻译为超文本标记语言<标签名>内容</标签名>静态网页动态网页的区别是看是否从数据中提取数据一般网页由几部分组分组成<html>开始标签 <head& ...

  5. 信贷业务(Ali)

    1.信贷业务视角 信贷业务主要有两个视角,借款人和出资机构.借款人关心借多少钱,还多少钱,多少利息:机构关心信贷资产风险,收益. 领域模型上两个视角分开:个人--->账单.机构--->资产 ...

  6. golang os.OpenFile

    os.O_WRONLY | os.O_CREATE | O_EXCL           [如果已经存在,则失败] os.O_WRONLY | os.O_CREATE                 ...

  7. PHP 适配器模式

    适配器模式(Adapter)模式:将一个类的接口,转换成客户期望的另一个类的接口.适配器让原本接口不兼容的类可以合作无间.     [适配器模式中主要角色]目标(Target)角色:定义客户端使用的与 ...

  8. AOS – 另外一个独特的页面滚动动画库(CSS3)

    AOS 是一个用于在页面滚动的时候呈现元素动画的工具库,你可能会觉得它和 WOWJS 一样,的确他们效果是类似的.但是AOS是 CSS3 动画驱动的库,当你滚动页面的时候能让元素动起来,当页面滚回顶部 ...

  9. JS高程5.引用类型(1)Object类型

    引用类型 在ECMASCript中,引用类型是一种数据结构,将数据和功能组织在一起,引用类型有时候也被称为对象定义,因为它们描述的是一类对象所具有的属性和方法.(注意:尽管ECMAScript从技术上 ...

  10. 一步步实现ABAP后台导入EXCEL到数据库【2】

    前文:http://www.cnblogs.com/hhelibeb/p/5912330.html 既然后台作业只能在应用服务器运行,那么,我们可以先将要上传的数据保存在应用服务器中,之后再以后台作业 ...