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.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.

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

  1. [leetcode shell]192. Word Frequency

    统计words.txt中每个单词出现的次数并排序 解法1: cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{prin ...

  2. LeetCode(605,581,566)

    LeetCode(605,581,566) 摘要:605盲改通过:581开始思路错误,后利用IDE修改(多重循环跳出方法):566用C语言时需要动态内存分配,并且入口参数未能完全理解,转用C++. 6 ...

  3. Aspose office (Excel,Word,PPT),PDF 在线预览

    前文: 做个备份,拿的是试用版的 Aspose,功能见标题 代码: /// <summary> /// Aspose office (Excel,Word,PPT),PDF 在线预览 // ...

  4. 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 ...

  5. c++ LeetCode(初级数组篇)十一道算法例题代码详解(一)

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/10940636.html 唉!最近忙着面试找实习,然后都是面试的很多是leetcode的算法题, ...

  6. LeetCode 192. Word Frequency

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

  7. LeetCode(194.Transpose File)(awk进阶)

    194. Transpose File Given a text file file.txt, transpose its content. You may assume that each row ...

  8. 192 Word Frequency

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

  9. SpringBoot入门Demo(Hello Word Boot)

    Spring Boot 是由Pivotal团队提供的全新框架,其设计目的是用来简化新的Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. ...

随机推荐

  1. yoj维护

    维护 启动容器 docker start yoj 暂停容器 docker stop yoj 重启容器 docker restart yoj 进入容器的终端 docker attach yok 保存容器 ...

  2. php unset对json_encode的影响

    先运行一段php代码: $a = Array(0=>'hello world', 1=>'girl', 2=>'boy'); var_dump(json_encode($a)); u ...

  3. Hdoj 1392.Surround the Trees 题解

    Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...

  4. python3 元组tuple

    元组应小括号()表示: tuple(seq)将列表转换为一个元组: 不可变的,元组是不可变,但元素可以是可变数据类型: 可以迭代,可以切片: +连接元组,*复制元组: del不可以删除元素,但可以删除 ...

  5. Java 枚举 的学习

    在JDK5.0之后,引进了一种与C语言相通的枚举类型. 所谓枚举类型就是指含有一组具有固定值, 并且容量有限的数据集合. 例如,定义一个星期的枚举类型, 从周一到周日是具有固定大小和固定值的集合 pu ...

  6. Mybatis Generator的model生成中文注释,支持oracle和mysql(通过修改源码的方式来实现)

    在看本篇之前,最好先看一下上一篇通过实现CommentGenerator接口的方法来实现中文注释的例子,因为很多操作和上一篇基本是一致的,所以本篇可能不那么详细. 首先说一下上篇通过实现Comment ...

  7. urllib的实现---cookie处理

    Cookie的使用 用 Python 来登录网站, 用Cookies记录登录信息, 然后就可以抓取登录之后才能看到的信息. 什么是cookies? Cookie,指某些网站为了辨别用户身份.进行ses ...

  8. Haproxy Nginx cluster构建

    -----client---------haproxy-------nginx1---------nginx2------192.168.1.250 192.168.1.1 192.168.1.10 ...

  9. luogu1484 种树 (优先队列)

    我每次都想选那个最大的.或者是它旁边的两个一起选,如果这两个一起选会大于那个最大的的话 那我就先把那个最大的选了,再提供一个反悔的选项(类似于网络流的思路?),就是我可以再把种的树换成它旁边那两个,也 ...

  10. NOIP2008双栈排序(贪心)

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...