shell: Word Frequecy

Question:

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

Note:Don't worry about handling ties, it is guaranteed that each word's frequency count is unique.
Hint:Could you write it in one-line using Unix pipes?

Solutions:

tr " " "\n" < words.txt |sed '/^$/d'| sort | uniq -c |sort -r | awk '{print $2,$1}'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,394评论 0 23
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,348评论 0 33
  • Basically we will reuse RA aspx pages for PFA Adding page...
    笑极阅读 1,456评论 0 0
  • 突然才觉得,那些曾经花时间陪伴过的人都成了过客,浪费了太多的时间在一些人一些事身上,路边的花也没有多好,但却忘记去...
    鹃儿阅读 858评论 0 2
  • 静坐电脑前 心乱如麻 必须做的事情很多 想做的事情更多 手指却一直在罢工 这个世界 我该如何游弋 这个社会 我该如何适应
    茜茜_1314_smile阅读 2,256评论 0 0