Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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.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:
declare -A arr #associative array
while IFS= read -r line
do
for word in $line
do
let arr[$word]=${arr[$word]}+1
done
done < words.txt
for key in ${!arr[@]}
do
echo $key ${arr[$key]}
done | sort -rn -k2
In our experience, we suggest you solve this Word Frequency LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Word Frequency LeetCode Solution
I hope this Word Frequency LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>