leetcode-shell-192. Word Frequency
2016-06-16 16:03
411 查看
Write a bash script to calculate the frequency of each word in a text file
For simplicity sake, you may assume:
Each word must consist of lowercase characters only.
Words are separated by one or more whitespace characters.
For example, assume that
Your script should output the following, sorted by descending frequency:
题意:统计词的频率
思路:
最开始想创建数组来记录词出现的频率,但是遍历成本高,后来看了别人的方法,使用awk工具就可以
最终shell脚本:
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -rn | awk '{print $2" "$1}'
tr 表示用什么切分单词,有点儿像split函数,后面跟空格和回车,表示我们通过空格和回车切分单词
sort 用来排序
uniq去重,-c用来统计个数
sort -rn -rn表示用数值倒序排序
words.txt.
For simplicity sake, you may assume:
words.txtcontains 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.txthas 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
题意:统计词的频率
思路:
最开始想创建数组来记录词出现的频率,但是遍历成本高,后来看了别人的方法,使用awk工具就可以
最终shell脚本:
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -rn | awk '{print $2" "$1}'
tr 表示用什么切分单词,有点儿像split函数,后面跟空格和回车,表示我们通过空格和回车切分单词
sort 用来排序
uniq去重,-c用来统计个数
sort -rn -rn表示用数值倒序排序
相关文章推荐
- Linux shell获取执行脚本文件所在的目录
- leetcode-shell-193. Valid Phone Numbers
- Xshell5下利用sftp上传下载传输文件
- 用 Data 定义变量
- Shell之awk详解
- shell if判断
- shell脚本收集
- Linux Shell系列教程
- PowerShell Pester 使用 - Assertion
- Shell ${}、##和%%使用范例
- Linux下的shell脚本的字符串截取
- Shell文件重定向的3种方法
- Linux下bash配置及执行顺序
- linux - python shell auto completion
- 4bash中对日期的使用
- 3对符合条件的文件和文件夹进行复制或修改
- 【shell】变量的配置文件
- 2文件及文件夹查找
- linux中环境变量配置bashrc与profile的区别
- linux shell 文件操作总结