Linux命令之sort

来源:互联网 发布:易建联nba数据虎扑 编辑:程序博客网 时间:2024/04/30 22:46
学习笔记:<linux shell编程>书籍

命令:sort
语法:sort [arg] [File]
功能:排序文件,对已经排序文件进行合并

常用参数选项:
-b, --ignore-leading-blanks ignore leading blanks
忽略前导空格和制表符,找出字段的第一或最后列
-d, --dictionary-order consider only blanks and alphanumeric characters
使用字典顺序排序,比较中仅考虑字母,数字和空格
-f, --ignore-case fold lower case to upper case characters
比较前将所有小写字母改成大写字母
-g, --general-numeric-sort compare according to general numerical value
-i, --ignore-nonprinting consider only printable characters
-M, --month-sort compare (unknown) < `JAN' < ... < `DEC'
-n, --numeric-sort compare according to string numerical value
按算数值对数字字段进行排序
-r, --reverse reverse the result of comparisons
颠倒指定排列的顺序

其它参数选项:
-c, --check check whether input is sorted; do not sort
-k, --key=POS1[,POS2] start a key at POS1, end it at POS2 (origin 1)
指定排序关键字
-m, --merge merge already sorted files; do not sort
只合并多个输入文件;假设输入文件已经排序
-o, --output=FILE write result to FILE instead of standard output
将输出指向参数指定的文件,而不是标准输出。Outfile 参数值可以与File参数值相同
-s, --stable stabilize sort by disabling last-resort comparison
-S, --buffer-size=SIZE use SIZE for main memory buffer
-t, --field-separator=SEP use SEP instead of non-blank to blank transition
指定Character为单一的字段分隔符
-T, --temporary-directory=DIR use DIR for temporaries, not $TMPDIR or /tmp;
multiple options specify multiple directories
将创建的所有临时文件放入Directory参数指定的目录中
-u, --unique with -c, check for strict ordering;
without -c, output only the first of an equal run
对排序后认为相同的行只保留其中一行
-z, --zero-terminated end lines with 0 byte, not newline
--help display this help and exit
--version output version information and exit


实例1. 使用sort 命令按照ASCII顺序排序文本
test@node-h:~/Jeff> cat fruits.txt
banana
orange
Persimmon
apple
%%banana
apple
ORANGE
123
test@node-h:~/Jeff> LANG=En_US sort fruits.txt
%%banana
123
ORANGE
Persimmon
apple
apple
banana
orange

解释:在这个例子中,排序规则是ASCII, %(百分比)->数字->大写字母->小写字母

实例2. 使用字典排序 (忽略特殊字符排序)
test@node-h:~/Jeff> sort -d fruits.txt
123
apple
apple
banana
%%banana
orange
ORANGE
Persimmon

解释:-d表示忽略特殊字符,只排序字母,数字和空格。在这个例子中,排序方式:数字->小写字母->大写字母
%%banana 被banana取代

实例3. 忽略大小写排序和忽略特殊字符排序
test@node-h:~/Jeff> sort -d -f fruits.txt
123
apple
apple
banana
%%banana
orange
ORANGE
Persimmon

解释:-d 表示忽略特殊字符排序,-f表示忽略大小写排序

实例4. 忽略大小写排序和忽略特殊字符排序,去除选项中的重复行
test@node-h:~/Jeff> sort -d -f -u fruits.txt
123
apple
banana
%%banana
orange
ORANGE
Persimmon

解释:-d 表示忽略特殊字符排序,-f表示忽略大小写排序, -u 表示去除重复行

实例5. 选定排序字段,选择分界符
test@node-h:/> cat etc/group
at:!:25:
audio:x:17:pulse
bin:x:1:daemon
cdrom:x:20:
console:x:21:
daemon:x:2:
dialout:x:16:test
disk:x:6:
floppy:x:19:
ftp:x:49:
games:x:40:
gdm:!:111:
haldaemon:!:102:
kmem:x:9:
lp:x:7:
mail:x:12:
maildrop:!:59:
man:x:62:
messagebus:!:101:
modem:x:43:
mysql:!:105:
news:x:13:
nobody:x:65533:
nogroup:x:65534:nobody
ntadmin:!:71:
ntp:!:106:
polkituser:!:107:
postfix:!:51:
public:x:32:
pulse:!:108:
pulse-access:!:109:
root:x:0:
sfcb:!:104:root
shadow:x:15:
sshd:!:65:
suse-ncc:!:110:
sys:x:3:
trusted:x:42:
tty:x:5:
utmp:x:22:
uucp:x:14:
uuidd:!:103:
video:x:33:test
wheel:x:10:
www:x:8:
xok:x:41:
users:x:100:

test@node-h:/> sort -t: -k3 -n /etc/group
root:x:0:
bin:x:1:daemon
daemon:x:2:
sys:x:3:
tty:x:5:
disk:x:6:
lp:x:7:
www:x:8:
kmem:x:9:
wheel:x:10:
mail:x:12:
news:x:13:
uucp:x:14:
shadow:x:15:
dialout:x:16:test
audio:x:17:pulse
floppy:x:19:
cdrom:x:20:
console:x:21:
utmp:x:22:
at:!:25:
public:x:32:
video:x:33:test
games:x:40:
xok:x:41:
trusted:x:42:
modem:x:43:
ftp:x:49:
postfix:!:51:
maildrop:!:59:
man:x:62:
sshd:!:65:
ntadmin:!:71:
users:x:100:
messagebus:!:101:
haldaemon:!:102:
uuidd:!:103:
sfcb:!:104:root
mysql:!:105:
ntp:!:106:
polkituser:!:107:
pulse:!:108:
pulse-access:!:109:
suse-ncc:!:110:
gdm:!:111:
nobody:x:65533:
nogroup:x:65534:nobody

解释:-t: 定义排序分隔符号是冒号(:), 如果没有设定,则默认是空白字符;-k3 比较第3个字段; -n 按照整数数字进行比较

实例6. sort 不稳定性演示
test@node-h:~/Jeff> cat num.txt
1:2: 3
1:2
3:4:5
2:7
test@node-h:~/Jeff> sort -t: -k1 num.txt
1:2
1:2: 3
2:7
3:4:5

解释:这个例子是设定按照第一行排列,但是排列后,原先第一行的数字1:2: 3却到了第二行了








0 0