---
title: "使用Shell对数字按照出现次数排序"
author: "liudaoru"
date: "2011-02-11T18:57:08Z"
tags: []
categories: ["操作系统"]
slug: "shell"
draft: false
lastmod: "2011-02-11T18:57:08Z"
id: 904077
url: "https://liudaoru.iteye.com/blog/904077"
---

今天遇到一个需求：

      在一个文本中有上万的id，需要对这些id按照出现次数进行排序。

 

下面是一种实现方法：

 

```
cat ids.txt | sort | uniq -c | sort --key=1,7 -r -n > uids_sort.txt
```

 

其中 uniq -c 是对连续出现的id进行累计计算，并把总数放到前面。

 

sort --key=1,7 的作用是窃取结果的前七列进行排序， -r 是反序， -n是作为数字排序。

 

经过一系列的操作，即可得到结果id。