# 192. 统计词频

### Link

{% embed url="<https://leetcode.cn/problems/word-frequency/>" %}

### Problem

写一个 bash 脚本以统计一个文本文件 words.txt 中每个单词出现的频率。

为了简单起见，你可以假设：

words.txt只包括小写字母和 ' ' 。 每个单词只由小写字母组成。 单词间由一个或多个空格字符分隔。

文本：

{% code overflow="wrap" %}

```
the day is sunny the the
the sunny is is
```

{% endcode %}

输出：

{% code overflow="wrap" %}

```
the 4
is 3
sunny 2
day 1
```

{% endcode %}

提示：

* 不要担心词频相同的单词的排序问题，每个单词出现的频率都是唯一的。
* 你可以使用一行 [Unix pipes](http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-4.html) 实现吗？

### Solution

{% tabs %}
{% tab title="Bash" %}

```bash
cat words.txt | tr -s ' ' '\n' |sort |uniq -c |sort -r | awk '{print $2, $1}'
```

{% endtab %}
{% endtabs %}
