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的更多相关文章

  1. Individual Project - Word frequency program-11061171-MaoYu

    BUAA Advanced Software Engineering Project:  Individual Project - Word frequency program Ryan Mao (毛 ...

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

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

  3. LeetCode(192. Word Frequency)

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

  4. [LeetCode] Word Frequency 单词频率

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

  5. [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. 这道题让我们找书中单词出现 ...

  6. 192 Word Frequency

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

  7. LeetCode 192. Word Frequency

    分析 写bash,不太会啊…… 难度 中 来源 https://leetcode.com/problems/word-frequency/ 题目 Write a bash script to calc ...

  8. Individual Project - Word frequency program - Multi Thread And Optimization

    作业说明详见:http://www.cnblogs.com/jiel/p/3978727.html 一.开始写代码前的规划: 1.尝试用C#来写,之前没有学过C#,所以打算先花1天的时间学习C# 2. ...

  9. Individual Project - Word frequency program——12061154Joy

    Description&Requirement: http://www.cnblogs.com/jiel/p/3978727.html 项目时间估计 理解项目要求: 1h 构建项目逻辑: 1h ...

随机推荐

  1. C#中调用C++的dll的参数为指针类型的导出函数(包括二级指针的情况)

    严格来说这篇文章算不上C++范围的,不过还是挂了点边,还是在自己的blog中记录一下吧. C++中使用指针是家常便饭了,也非常的好用,这也是我之所以喜欢C++的原因之一.但是在C#中就强调托管的概念了 ...

  2. MyISAM 和InnoDB区别

    MyISAM 和InnoDB 讲解 InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级处理 ...

  3. 使用JavaScript实现一个倒数计时程序

    使用JavaScript在网页中实现一个计算当年还剩多少时间的倒数计时程序,网页上能够实时动态显示“XX年还剩XX天XX时XX分XX秒”: 程序代码如下: <meta charset=" ...

  4. c#, 输出二进制

    int x=-17; string str= Convert.ToString(x,2);Debug.Log(str); 输出结果: 11111111111111111111111111101111 ...

  5. SSH客户端(如PuTTY)ssh远程登录Linux非常慢的解决方法

    转:http://blog.useasp.net/archive/2014/05/19/solved-the-problem-of-ssh-client-such-as-putty-remote-lo ...

  6. Linux-NTP-Server+Client

    GMT/UTC/CST;/etc/localtime,/usr/share/zoneinfo/*时区文件,/etc/profile加TZ变量;硬件时间RTC,系统时间;date,hwclock,tzs ...

  7. JQuery Pagenation 知识点整理——arguments,callee,caller,apply应用(20150517)(转)

    arguments 该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments[n]参数function :选项.当前正在执行的 Function 对象的名字. n :选 ...

  8. PHP 开发环境配置

    使用phpStudy +Zend Studio 使用phpStudy +Zend Studio ,这个组合是我个人使用的比较好的,现在分享出来.一.phpStudy简体中文版 该程序包集成最新的Apa ...

  9. Android ImageButton android:scaleType

    ImageView的属性android:scaleType,即 ImageView.setScaleType(ImageView.ScaleType). android:scaleType是控制图片如 ...

  10. HTML 列表

    HTML 支持有序.无序和定义列表 实例 无序列表 本例演示无序列表. 有序列表 本例演示有序列表. (可以在本页底端找到更多实例.) 无序列表 无序列表是一个项目的列表,此列项目使用粗体圆点(典型的 ...