Nec*_*cto 14 unix linux sorting shell
我正在尝试对以下文件进行排序:
a 2
b 1
a 10
Run Code Online (Sandbox Code Playgroud)
我需要得到:
a 2
a 10
b 1
Run Code Online (Sandbox Code Playgroud)
我知道-kPOS [opts]选项,并尝试使用它:
sort -k1 -k2n file
Run Code Online (Sandbox Code Playgroud)
但是这个命令只给我:
a 10
a 2
b 1
Run Code Online (Sandbox Code Playgroud)
所以它按第一列排序,但第二列没有排序.sort -k2n file在第二列中运行排序.
b 1
a 2
a 10
Run Code Online (Sandbox Code Playgroud)
我怎么能按两列排序呢?
sort(GNU coreutils)5.93
cho*_*oba 28
您必须终止主键,否则,sort使用从给定的一个开始的所有字段:
sort -k1,1 -k2n
Run Code Online (Sandbox Code Playgroud)
如果你有GNU sort排序,那么你可以做一个版本排序:
$ sort -V file
a 2
a 10
b 1
Run Code Online (Sandbox Code Playgroud)
选项:
-V, --version-sort natural sort of (version) numbers within text
Run Code Online (Sandbox Code Playgroud)
版本排序的好处是无论列如何都可以使用:
$ cat file
a2
b1
a10
$ sort -V file
a2
a10
b1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30413 次 |
| 最近记录: |