-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathword_frequency.sh
67 lines (60 loc) · 1.13 KB
/
word_frequency.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'''
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.
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
'''
cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{ print $2, $1 }'
'''
explanation:
➜ Desktop cat Words.txt
the day is sunny the the
the sunny is is
➜ Desktop cat Words.txt| tr -s ' ' '\n'
the
day
is
sunny
the
the
the
sunny
is
is
➜ Desktop cat Words.txt| tr -s ' ' '\n' | sort
day
is
is
is
sunny
sunny
the
the
the
the
➜ Desktop cat Words.txt| tr -s ' ' '\n' | sort | uniq -c
1 day
3 is
2 sunny
4 the
➜ Desktop cat Words.txt| tr -s ' ' '\n' | sort | uniq -c | sort -r
4 the
3 is
2 sunny
1 day
➜ Desktop cat Words.txt| tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{print $2, $1}'
the 4
is 3
sunny 2
day 1
'''