Best way to simulate “group by” from bash?

前端 未结 14 1053
半阙折子戏
半阙折子戏 2020-11-29 15:03

Suppose you have a file that contains IP addresses, one address in each line:

10.0.10.1
10.0.10.1
10.0.10.3
10.0.10.2
10.0.10.1

You need a

14条回答
  •  执笔经年
    2020-11-29 15:47

    The quick and dirty method is as follows:

    cat ip_addresses | sort -n | uniq -c

    If you need to use the values in bash you can assign the whole command to a bash variable and then loop through the results.

    PS

    If the sort command is omitted, you will not get the correct results as uniq only looks at successive identical lines.

提交回复
热议问题