Sort命令既可以从特定的文件,也可以从stdin获取输入,并将输出写入stdout
[root@mio-253 shells]# sort b.txt a.txt c.txt > sorted.txt
[root@mio-253 shells]# cat sorted.txt
aaaa
bbbbbbb
ccccc
找出已排序文件中不重复的行
[root@mio-253 shells]# cat sorted.txt | uniq> uniq.txt
[root@mio-253 shells]# cat uniq.txt
aaaa
sdsds
bbbbbbb
ccccc
逆向排序
[root@mio-253 shells]# sort -r a.txt b.txt c.txt > rsort.txt
[root@mio-253 shells]# cat rsort.txt
ccccc
bbbbbbb
aaaa
检查文件是否已经被排序过
[root@mio-253 shells]# sort -C sorted.txt
[root@mio-253 shells]# echo $?
1
[root@mio-253 shells]# sort -C a.txt
[root@mio-253 shells]# echo $?
0
合并二个文件
[root@mio-253 shells]# sort -m a.txt b.txt
aaaa
bbbbbbb
根据某列排序
[root@mio-253 shells]# sort -nk 1 b.txt
1 bbbbbbb
2 ddd
3 fff
6 eee
Uniq
不显示重复的行
Uniq a.txt
只显示唯一行
Uniq –u a.txt
统计各行出现的次数
[root@mio-253 shells]# uniq -c sorted.txt
2 aaaa
1 sdsds
1 bbbbbbb
1 ccccc
找出重复的行
[root@mio-253 shells]# uniq -d sorted.txt
Aaaa
我们可以结合-s –w来指定键
-s 指定可以跳过前N个字符
-w 指定用于比较的最大字符数
上一篇:校验和与核实文件 下一篇:快递查询API