Original: https://leetcode.com/problems/word-frequency/Write a bash script to calculate the frequency of each wordinchA textfilewords.txt.For simplicity sake, you could assume:words.txt contains only lowercase characters and space' 'characters. Each of the word must consist of lowercase characters only. Words is separated by one or Morewhitespace characters. For example, assume that words.txt have the following content:the day is sunny the thethe sunny are isyour script should out Put the following, sorted by descending frequency:the4 is3Sunny2 Day1
Simple point is to give you a file, inside is a space "can have multiple" divided word "all lowercase", please count the number of different words, and output in descending order.
The words are recorded in the array, then the output is traversed, the sort is sorted, and the 2nd column is numbered in descending order.
Answer:
awk ' {for (i=1;i<=nf;i++) num[$i]++;} end{for (k in num) print K "" num[k]}' words.txt| Sort2
Use sort sorting to specify which column uses-K, followed by numbers, sorted by numbers, use-N, and use-R in descending order.
Shell exercises count the number of words in descending order