名称:sort
位置:/usr/bin/sort
权限:所有用户
用法: sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F
选项:
-b --ignore-leading-blanks 忽略最前面的空格
-f --ignore-case fold lower case to upper case characters 忽略字母的大小写
-M --month-sort compare (unknown) < `JAN' < ... < `DEC' 以月份的名字来排序
-n --numeric-sort compare according to string numerical value 以纯数字排序 (默认以文本类型排序)
复制代码代码如下:
$ cat a
11
32
5
1
4
3
$ sort a
1
11
3
32
4
5
$ sort -n a
1
3
4
5
11
32
-u --unique 相同的数据中,仅出现一行
$ cat a
1
3
5
1
3
$ sort -u a
1
3
5
-t --field-separator=SEP use SEP instead of non-blank to blank transition 指定分隔符,默认以TAB为分隔符
-k --key=POS1[,POS2] start a key at POS1 (origin 1), end it at POS2 (default end of line). See POS syntax below
指定排序的区间
以冒号":"作为分隔符的第一个字段排序
复制代码代码如下:
head /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
$ head /etc/passwd| sort -t ":" -k 1
bin:x:2:2:bin:/bin:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
games:x:5:60:games:/usr/games:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
root:x:0:0:root:/root:/bin/bash
sync:x:4:65534:sync:/bin:/bin/sync
sys:x:3:3:sys:/dev:/bin/sh
以下这个例子可以看出-n的重要性了
$ cat a
pa:11:a
sa:32:c
app:5:b
stort:1:d
pear:4:aa
hello:3:f
$ cat a |sort -t ":" -k 2
pa:11:a
stort:1:d
sa:32:c
hello:3:f
pear:4:aa
app:5:b
$ cat a |sort -t ":" -k 2 -n
stort:1:d
hello:3:f
pear:4:aa
app:5:b
pa:11:a
sa:32:c