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}'
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,011評(píng)論 0 23
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,890評(píng)論 0 33
  • Basically we will reuse RA aspx pages for PFA Adding page...
    笑極閱讀 243評(píng)論 0 0
  • 突然才覺得,那些曾經(jīng)花時(shí)間陪伴過的人都成了過客,浪費(fèi)了太多的時(shí)間在一些人一些事身上,路邊的花也沒有多好,但卻忘記去...
    鵑兒閱讀 143評(píng)論 0 2
  • 靜坐電腦前 心亂如麻 必須做的事情很多 想做的事情更多 手指卻一直在罷工 這個(gè)世界 我該如何游弋 這個(gè)社會(huì) 我該如何適應(yīng)
    茜茜_1314_smile閱讀 320評(píng)論 0 0

友情鏈接更多精彩內(nèi)容