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. 让图片完全显示出来,应对overflow,以及在背景中完全显示出来

    1.应对overflow <script type="text/javascript"> $('.foo img').css('width','100%');//修正, ...

  2. 主成分分析(PCA)

    主成分分析(principal component analysis)是一种常见的数据降维方法,其目的是在"信息"损失较小的前提下,将高维的数据转换到低维,从而减小计算量.PCA的 ...

  3. Object-c 语言

    字符串操作: http://www.myexception.cn/mobile/455287.html 1,判断两字符串是否相同 NSString *str1 = @"hello pepe& ...

  4. Unity3D研究院之Inspector面板枚举的别名与排序

    虽然mono是支持unicode的.可以在枚举里写中文,但是我还是觉得写英文好一些.可是在编辑器上策划是希望看到的是中文的,还有就是枚举的展示排序功能,策划在编辑的时候为了方便希望把常用的枚举排上前面 ...

  5. 深入浅出Attribute (转载)

    原文地址:http://blog.csdn.net/FantasiaX/article/details/1627694 正文: 什么是Attribute?Attribute是干什么使的?Attribu ...

  6. libc-glibc

    glibc 和 libc 都是 Linux 下的 C 函数库. libc 是 Linux 下的 ANSI C 函数库:glibc 是 Linux 下的 GUN C 函数库. ANSI C 和 GNU ...

  7. linux服务之udevd

    http://www.ibm.com/developerworks/cn/linux/l-cn-udev/[root@localhost ~]# uname -r2.6.32-431.el6.x86_ ...

  8. Advancing The Realtime Web With RethinkDB

    RethinkDB is an open-source distributed database built to store JSON and scale with very little effo ...

  9. 任我行 CRM 9.4

    下载地址: http://ftp.wecrm.com/azb/graspcrm_ect_v9.4.rar 200用户补丁下载地址: http://bbs.sunwy.org/thread-197862 ...

  10. ShopEX 4.8.5.81822 前台Getshell

    ShopEX 4.8.5.81822 前台Getshell 作者:unhonker   发布:2014-06-23 00:12   分类:漏洞公布   被撸:8,179次   抢沙发     利用方式 ...