如何在unix中按字母和数字在不同字段中对文件进行排序? [英] How to sort a file in unix both alphabetically and numerically on different fields?

查看:25
本文介绍了如何在unix中按字母和数字在不同字段中对文件进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请不要认为这是在 unix 中对字母数字数据进行排序"问题的重复...我看了其他答案,认为我的情况有点不同!

我有这样的数据:

A 192112D 188091一个 281乙 919

...我想对第 1 列(按字母顺序)排序,然后按第 2 列(按数字排序).我尝试使用:

sort -n -k1,2

...但是这让我对第一个字段进行了正确排序,但是对第二个字段进行了错误的排序(1000,1002,1003,10,1 ...而不是 1,10,1000,1002,1003).

有人可以建议如何按照我想要的方式对这两列进行排序吗?

解决方案

试试这样使用:-

sort -k1,1 -k4,4n

<块引用>

  • -n : 使程序按数值排序
  • -k opts:使用给定的列号对数据/字段进行排序.例如,选项 -k 2 使程序使用第二个
    数据列.选项 -k 3,3n -k 4,4n 对每一列进行排序.第一
    它将对第 3 列和第 4 列进行排序.

Please don't think this is a repeat of the "Sorting alphanumeric data in unix" question... I looked at the other answers, and think my case is a bit different!

I have data like this:

A    192
D    112
D    188
C    091
A    281
B    919

...And I want to sort first column 1 (alphabetically), and then by column 2 (numerically). I tried using:

sort -n -k1,2

...But this gave me correctly sorted for the first field, but then the wrong sorting for the second field (1000,1002,1003,10,1 ... instead of 1,10,1000,1002,1003).

Can someone please suggest how to sort these two columns the way I'd like?

解决方案

Try using like this:-

sort -k1,1 -k4,4n

  • -n : Makes the program sort according to numerical value
  • -k opts: Sort data / fields using the given column number. For example, the option -k 2 made the program sort using the second
    column of data. The option -k 3,3n -k 4,4n sorts each column. First
    it will sort 3rd column and then 4th column.

这篇关于如何在unix中按字母和数字在不同字段中对文件进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆