> For the complete documentation index, see [llms.txt](https://valineliu.gitbook.io/deuterium-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://valineliu.gitbook.io/deuterium-wiki/linux/awk-1/ge-shu-tong-ji.md).

# 个数统计

对文本中指定项目进行个数统计。

比如一个文件：

{% code title="original\_file" %}

```bash
a
b
a
c
d
e
a
e
d
a
b
c
a
```

{% endcode %}

每行一个字母，要统计每个字母出现的次数。可以：

```bash
awk '{sum[$0]+=1} END {for(k in sum) print k ":" sum[k]}' original_file
```

结果：

```bash
a:5
b:2
c:2
d:2
e:2
```

当然也可以放进一个脚本中：

{% code title="group\_by.awk" %}

```bash
#! /usr/bin/awk -f
{sum[$0]+=1} END {for(k in sum) print k ":" sum[k]}
```

{% endcode %}
