LeetCode(192. Word Frequency)
192. 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.txtcontains only lowercase characters and space' 'characters.- Each word must consist of lowercase characters only.
- Words are separated by one or more whitespace characters.
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
Note:
- Don't worry about handling ties, it is guaranteed that each word's frequency count is unique.
- Could you write it in one-line using Unix pipes?
一、首先考虑用到grep
- -oE:表示将原文本内容变成一个单词一行的存储方式

二、排序sort(这里排序是为了后面好去重)

三、去重并计算单词出现次数

四、再sort排序(-nr表示按数值进行降序排序)

五、再通过awk控制输出方式

cat words.txt |grep -oE '[a-z]+' |sort |uniq -c |sort -nr |awk '{print $2" "$1}'
法二:tr
注意:tr -s:表示如果发现连续字符,就把他们缩减成1个;后面的' ' '\n'是空格和回车:表示把所有空格换成回车。
tr -s ' ' '\n' < words.txt |sort |uniq -c|sort -nr |awk '{print $2" "$1}'
法三:awk
#!/bin/bash
awk '{
for (i = ; i < NF; ++i) ++s[$i];
} END {
for (i in s) print i, s[i];
}' words.txt |sort -nr -k 2

参考资料:http://www.cnblogs.com/grandyang/p/5386475.html
LeetCode(192. Word Frequency)的更多相关文章
- [leetcode shell]192. Word Frequency
统计words.txt中每个单词出现的次数并排序 解法1: cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{prin ...
- LeetCode(605,581,566)
LeetCode(605,581,566) 摘要:605盲改通过:581开始思路错误,后利用IDE修改(多重循环跳出方法):566用C语言时需要动态内存分配,并且入口参数未能完全理解,转用C++. 6 ...
- Aspose office (Excel,Word,PPT),PDF 在线预览
前文: 做个备份,拿的是试用版的 Aspose,功能见标题 代码: /// <summary> /// Aspose office (Excel,Word,PPT),PDF 在线预览 // ...
- bit,Byte,Word,DWORD(DOUBLE WORD,DW)
1个二进制位称为1个bit,8个二进制位称为1个Byte,也就是1个字节(8位),2个字节就是1个Word(1个字,16位),则DWORD(DOUBLE WORD)就是双字的意思,两个字(4个字节/3 ...
- c++ LeetCode(初级数组篇)十一道算法例题代码详解(一)
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/10940636.html 唉!最近忙着面试找实习,然后都是面试的很多是leetcode的算法题, ...
- LeetCode 192. Word Frequency
分析 写bash,不太会啊…… 难度 中 来源 https://leetcode.com/problems/word-frequency/ 题目 Write a bash script to calc ...
- LeetCode(194.Transpose File)(awk进阶)
194. Transpose File Given a text file file.txt, transpose its content. You may assume that each row ...
- 192 Word Frequency
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- SpringBoot入门Demo(Hello Word Boot)
Spring Boot 是由Pivotal团队提供的全新框架,其设计目的是用来简化新的Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. ...
随机推荐
- 【XSY2111】Chef and Churus 分块 树状数组
题目描述 有一个长度为\(n\)的数组\(A\)和\(n\)个区间\([l_i,r_i]\),有\(q\)次操作: \(1~x~y\):把\(a_x\)改成\(y\) \(2~x~y\):求第\(l\ ...
- 【BZOJ3771】Triple 生成函数 FFT 容斥原理
题目大意 有\(n\)把斧头,不同斧头的价值都不同且都是\([0,m]\)的整数.你可以选\(1\)~\(3\)把斧头,总价值为这三把斧头的价值之和.请你对于每种可能的总价值,求出有多少种选择方案. ...
- MySql的CURRENT_TIMESTAMP
在创建时间字段的时候 DEFAULT CURRENT_TIMESTAMP表示当插入数据的时候,该字段默认值为当前时间 ON UPDATE CURRENT_TIMESTAMP表示每次更新这条数据的时候, ...
- MySQL 报错 1055
具体报错 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'exer.student.sid' w ...
- php+redis配置
系统环境: win10+phpstudy+lamp 安装扩展 php5.6.4 =>下载地址:http://windows.php.net/downloads/pecl/releases/red ...
- shell getopts用法
eg:sh test.sh -u tom -p 123456: getopts的使用形式:getopts OPTION_STRING VAR: OPTION_STRING:-u,-p这种自定义选项: ...
- Mysql数据库操作笔记
如果数据库表字段存在,则删除该表 drop table if exists `table_name` 创建数据库表语句 create table `table_name`( `id` ) not n ...
- [JSOI2008]Blue Mary的职员分配
由于Blue Mary呕心沥血的管理,Blue Mary的网络公司蒸蒸日上.现在一共拥有了n名职员,可惜没有任何的金钱和声誉.平均每名每天职员都可以给公司带来x单位金钱或者y单位声誉(名利不能双全). ...
- Educational Codeforces Round 46 C - Covered Points Count
C - Covered Points Count emmm 好像是先离散化一下 注意 R需要+1 这样可以确定端点 emmm 扫描线?瞎搞一下? #include<bits/stdc++.h&g ...
- OpenLayers学习笔记(一)—在线加载谷歌影像地图&离线加载本地瓦片地图
实现根据在线离线判断加载地图, 在线加载谷歌影响地图, 离线加载本地瓦片地图 作者: 狐狸家的鱼 Github: 八至 html代码 <div id="map" tabind ...