linux的uniq和sort命令使用

环境:centos

1、uniq命令解析

[root@deam uniq]# cat file.txt   #原文件
123
123
134
456
123
[root@deam uniq]#  uniq file.txt #删除所有连续的重复行,只显示唯一的行。
123
134
456
123
[root@deam uniq]# sort file.txt |uniq #删除所有重复的行
123
134
456
[root@deam uniq]# sort file.txt | uniq -d #显示文件中重复的行
123
[root@deam uniq]# sort file.txt | uniq -D #打印所有重复的行
123
123
123
[root@deam uniq]# sort file.txt | uniq -c #显示文件中每一行的出现次数
      3 123
      1 134
      1 456
[root@deam uniq]# sort file.txt | uniq -c | sort -nr #照每一行的出现次数进行排序
      3 123
      1 456
      1 134
[root@deam uniq]# uniq -d -w 2 file.txt #比较文件中的前2个字符,并显示重复行
123
[root@deam uniq]# uniq -d -s 2 file.txt #-s 选项来忽略比较前 N 个字符
123

2、sort常用命令

[root@deam uniq]# cat seq.txt  #原文件
1
2
10
3
5
4
5
6
7
12
8
9
10
11
[root@deam uniq]# sort seq.txt  #排序
1
10
10
11
12
2
3
4
5
5
6
7
8
9
[root@deam uniq]# sort -u seq.txt #排序后输出行中去除重复行
1
10
11
12
2
3
4
5
6
7
8
9
[root@deam uniq]# sort -r seq.txt  #默认的排序方式是升序,如果想改成降序 -r
9
8
7
6
5
5
4
3
2
12
11
10
10
1
[root@deam uniq]# sort -n seq.txt  #使用-n选项,来告诉sort,“要以数值来排序”!
1
2
3
4
5
5
6
7
8
9
10
10
11
12
[root@deam uniq]# sort -n seq.txt -o seq.txt #可以重定向内容到自己
[root@deam uniq]# cat seq.txt     #重定向后的文本
1
2
3
4
5
5
6
7
8
9
10
10
11
12
[rocrocket@deam uniq]$ sort -n -k 2 -t : facebook.txt #用-k来指定列数 sort提供了-t选项,后面可以设定间隔符。
apple:10:2.5
orange:20:3.4
banana:30:5.5
pear:90:2.3
做一个决定,并不难,难的是付诸行动,并且坚持到底。
原文地址:https://www.cnblogs.com/wukc/p/13455735.html