Skip to content

Instantly share code, notes, and snippets.

@nothingrealhappen
Last active August 29, 2015 14:18
Show Gist options
  • Save nothingrealhappen/2f3e8e37b4926b9f8b12 to your computer and use it in GitHub Desktop.
Save nothingrealhappen/2f3e8e37b4926b9f8b12 to your computer and use it in GitHub Desktop.
【每日一题】2015年3月31日
@nothingrealhappen
Copy link
Author

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

For simplicity sake, you may assume:

  1. words.txt contains only lowercase characters and space' ' characters.
  2. Each word must consist of lowercase characters only.
  3. 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment