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. windows刷新本机DNS缓存

    ipconfig /flushdns

  2. 自学Python之路-Python并发编程+数据库+前端

    自学Python之路-Python并发编程+数据库+前端 自学Python之路[第一回]:1.11.2 1.3

  3. 【UOJ#422】【集训队作业2018】小Z的礼物(min-max容斥,轮廓线dp)

    [UOJ#422][集训队作业2018]小Z的礼物(min-max容斥,轮廓线dp) 题面 UOJ 题解 毒瘤xzy,怎么能搬这种题当做WC模拟题QwQ 一开始开错题了,根本就不会做. 后来发现是每次 ...

  4. webpack入门(三)webpack的api

    终于到了webpack api这一篇,我都等不及了0.0; webpack is fed a configuration object. Depending on your usage of webp ...

  5. ImageMagick: win7 | win8 & uac (用户帐户控制) 注册表的一些事

    现在用win7,win8的人越来越多了, 程序在一些 win 7, win8 上运行会遇到一些之前没想过的兼容性问题. 比如 64位系统运行32位程序时的注册表重定向,还有因为 uac (用户帐户控制 ...

  6. 【非专业前端】使用vue2.5.17+element2.4.5

    开发工具:WebStorm 先搞好环境 可以看出,想安装@vue/cli需要node.js.先去下载安装好. 然后安装@vue/cli npm install -g @vue/clinpm insta ...

  7. eclipse导出svn源码,如何转化为项目

    1.先导出 2.点击项目右键,选“属性” 3.选择project facets 4.添加对应的支持 5.可进行进一步配置,设置name,然后点击确定等待完成

  8. mybatis的一些小知识

    <update id="setReaded"> update wa_ent_job_app set IS_READ= 1, READ_TIME=now() where ...

  9. js数组歌

    判断是不是数组,isArray最靠谱. 按照条件来判断,every/some给答案 是否包含此元素,includes最快速. find/findIndex很相似,按条件给第一个值. indexOf/l ...

  10. 3D游戏的角色移动

    * -----英雄的移动控制 * * * * */ using System.Collections; using System.Collections.Generic; using UnityEng ...