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:

Your script should output the following, sorted by descending frequency:

Note:
Don’t worry about handling ties, it is guaranteed that each word’s frequency count is unique.

我的Answer:

解释下:cat输出words.txt内容,awk行内操作的命令,判断每一行是否有一个元素,不是一个就用split进行分割,然后遍历记录到数组中,END,统一输出最后结果,遍历输出然后用管道排序,sort ,-r降序 -n按数字排序,-k2按照第二列排序。