N天学习一个linux命令之sort

用途

对文本内容按行排序,输出排好序后的内容到标准输出流

用法

sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F

常用选项

-b, --ignore-leading-blanks
忽略前导空格

-d, --dictionary-order
按字典排序

-f, --ignore-case
不区分大小写

-g, --general-numeric-sort
根据数字值比较

-i, --ignore-nonprinting
忽略不可见字符

-M, --month-sort
按月份排序

-h, --human-numeric-sort
按照人类可读数字进行比较,比如:1K, 2G

-n, --numeric-sort
根据字符串数字值比较

-R, --random-sort
随机排序

-r, --reverse
使用倒序显示

--sort=WORD
sort according to WORD: general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V

-V, --version-sort
natural sort of (version) numbers within text

--files0-from=F
read input from the files specified by NUL-terminated names in file F; If F is - then read names from stan-dard input

-k, --key=POS1[,POS2]
设置比较的字符串开始和结束位置,默认是从1到行结束

-m, --merge
合并已拍好序的文件时不排序

-o, --output=FILE
设置内容输出的文件,默认是直接输出

-s, --stable
stabilize sort by disabling last-resort comparison

-S, --buffer-size=SIZE
use SIZE for main memory buffer

-T, --temporary-directory=DIR
use DIR for temporaries, not $TMPDIR or /tmp; multiple options specify multiple directories

-u, --unique
输出内容去重

-z, --zero-terminated
end lines with 0 byte, not newline

实践

  1. 按数字降序排列
//排序前
[root@vm ~]# cat cn.log
s
a
b
A
1z
12r
dd
2a
//排序后
[root@vm ~]# sort -nr cn.log
12r
2a
1z
s
dd
b
A
a
  1. 不区分大小写,排序结果去重
[root@vm ~]# sort -f -u cn.log
12r
1z
2a
a
b
dd
s

参考资料

【1】man sort

原文地址:https://www.cnblogs.com/wadeyu/p/8583877.html