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 ...
随机推荐
- dede模板完全控制攻略
1.使用php代码 @me代表了当前字段的值 {dede:tagname runphp='yes'} @me = "123456";//如果使用了自定义函数 则@me得到的是函数返 ...
- 怎么在OCR文字识别软件中安装和启动 OCR文字识别软件 Hot Folder
默认情况下,ABBYY Hot Folder 会与 ABBYY FineReader 12 一起安装到计算机中.(关于ABBYY FineReader 12请参考ABBYY FineReader 12 ...
- ISV 和SI 是什么
ISV是Independent Software Vendors 的英文缩写,意为"独立软件开发商",特指专门从事软件的开发.生产.销售和服务的企业,如微软(Microsoft). ...
- 【转】UVa Problem 100 The 3n+1 problem (3n+1 问题)——(离线计算)
// The 3n+1 problem (3n+1 问题) // PC/UVa IDs: 110101/100, Popularity: A, Success rate: low Level: 1 / ...
- maven学习笔记(定制一个Web项目)
创建web项目: mvn archetype:generate -DgroupId=cn.net.comsys.ut4.simpleweb -DartifactId=simple-web -Dpack ...
- C#实现数字字符串左补齐0的3种方法
int n = 3; string s = n.ToString().PadLeft(4, '0'); //0003 s = string.Format("{0:d4}", n); ...
- innertext与innerhtml
<div id="test"> <span style="color:red">test1</span> test2 < ...
- 【shell】case语句
case只能判断一种条件关系,而if能判断多种条件关系 #!/bin/bash read -p "please input your choice (high/middle/low):&qu ...
- 使用 Windows PowerShell 来管理和开发 windowsazure.cn 账户的特别注意事项
6月6日,微软面向中国大陆用户开放了Microsoft Azure公众预览版的申请界面.大家可以申请免费的 beta 试用,收到内附邀请码的通知邮件后只需输入激活码即可开始免费试用.具体网址为: ht ...
- 【Java编程进阶-1】enum枚举的使用
枚举主要用于枚举常量,下面举个简单的应用. 比如一个公司有如下几个部门: 研发部: 销售部: 财务部: (其他部门暂时不列举) 部门的某些信息相对固定,此时可以考虑使用枚举来说明: 枚举类 Depts ...