Word Frequency
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
# Read from the file words.txt and output the word frequency list to stdout. cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -rn | awk '{print $2" "$1}'
tr -s: 使用指定字符串替换出现一次或者连续出现的目标字符串(把一个或多个连续空格用换行符代替)
sort: 将单词从小到大排序
uniq -c: uniq用来对连续出现的行去重,-c参数为计数
sort -rn: -r 倒序排列, -n 按照数值大小排序(感谢网友 长弓1990 指正)
awk '{ print $2, $1 }': 格式化输出,将每一行的内容用空格分隔成若干部分,$i为第i个部分。
本文链接:http://bookshadow.com/weblog/2015/03/24/leetcode-word-frequency/
Word Frequency的更多相关文章
- Individual Project - Word frequency program-11061171-MaoYu
BUAA Advanced Software Engineering Project: Individual Project - Word frequency program Ryan Mao (毛 ...
- [Bash]LeetCode192. 统计词频 | Word Frequency
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- LeetCode(192. Word Frequency)
192. Word Frequency Write a bash script to calculate the frequency of each word in a text file words ...
- [LeetCode] Word Frequency 单词频率
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- [CareerCup] 17.9 Word Frequency in a Book 书中单词频率
17.9 Design a method to find the frequency of occurrences of any given word in a book. 这道题让我们找书中单词出现 ...
- 192 Word Frequency
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- LeetCode 192. Word Frequency
分析 写bash,不太会啊…… 难度 中 来源 https://leetcode.com/problems/word-frequency/ 题目 Write a bash script to calc ...
- Individual Project - Word frequency program - Multi Thread And Optimization
作业说明详见:http://www.cnblogs.com/jiel/p/3978727.html 一.开始写代码前的规划: 1.尝试用C#来写,之前没有学过C#,所以打算先花1天的时间学习C# 2. ...
- Individual Project - Word frequency program——12061154Joy
Description&Requirement: http://www.cnblogs.com/jiel/p/3978727.html 项目时间估计 理解项目要求: 1h 构建项目逻辑: 1h ...
随机推荐
- kmeans算法
# coding:utf-8 import numpy as np import matplotlib.pyplot as plt def dis(x, y): #计算距离 return np.sum ...
- C# 调用 C++ dll (类型对照)
函数调用导致堆栈不对称.原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配. 在dllimport中加入CallingConvention参数就行了,[DllImport(PCAP_DLL ...
- Jquery easyui的validatebox控件和正则表达式
http://blog.csdn.net/dandanzmc/article/details/36421465 仔细观察jquery.validatebox.js文件,会发现它的验证其实还是采用的正则 ...
- mysql提权笔记
最近小菜遇到mysql提权,总是会搞错,就记记笔记吧!以后方便用 先说手工吧! mysql<5.0,导出路径随意:5.0<=mysql<5.1,则需要导出至目标服务器的系统目录(如: ...
- jquery的$.extend和$.fn.extend作用及区别.txt
jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(); jQuery.extend(); (1)类级别 类级别你可以理解为拓展jquery类,最明显的例子是$.ajax ...
- Dynamics CRM 2011 JScript
if (!this.JSON) { this.JSON = {}; } (function () { function f(n) { return n < 10 ? '0' + n : n; } ...
- adb uninstall/pull/push 命令的使用总结
uninstall的使用 adb uninstall package_name 卸载软件adb -k uninstall package_name 完全删除,包括data/data文件夹也删掉. pu ...
- My second "last working day"
时间真快,转眼硕士毕业已经快8年了. 今天是我的第二个last working day.也是我即将结束在外企工作的节点. 说来,毕业的时候,找工作,在确定了最后要去的单位之前,手头已经拿过了10家单位 ...
- 纠结的NTP安装过程
为了部署实验用的openstack环境,其中有NTP的安装环节.在这个过程中,真是折腾了一下午...遇到了一些问题! 由于公司内部网络管理的原因,很多网站没有办法访问,比如公开的时间服务站点,我找了几 ...
- android学习笔记33——资源ShapeDrawable
ShapeDrawable ShapeDrawable用于定义一个基本的几何图像(如,矩形.圆形.线条.......). 定义ShapeDrawable的XML文件的根元素是<shape.../ ...