shell脚本之排序工具(sort、uniq)

一、sort 工具

(1)sort 是一个以行为单位对文件内容进行排序的工具,也可以根据不同的数据类型来排序
(2)sort 命令格式:sort [选项] 参数
(3)常用的选项:
-f:忽略大小写;
-b:忽略每行前面的空格;
-M:按照月份进行排序;
-n:按照数字进行排序;
-r:反向排序;
-u:等同于 uniq,表示相同的数据仅显示一行;
-t:指定分隔符,默认使用[Tab]键分隔;
-o <输出文件>:将排序后的结果转存至指定文件;
-k:指定排序区域;

示例1:将 /etc/passwd 文件中的账号进行排序

sort /etc/passwd

示例2:将/etc/passwd 文件中的第 4 列进行反向排序

sort -t : -rk 4 /etc/passwd

示例3:将 etc/passwd文件中第 4 列进行排序,并将输出内容保存至test.txt 文件中

sort -t : -k 4 /etc/passwd -o test.txt

二、uniq 工具

(1)uniq 工具在 Linux 系统中通常与 sort 命令结合使用,用于报告或者忽略文件中的重复行
(2)命令语法格式为:uniq [选项] 参数
(3)常用选项包括:

-c:进行计数;
-d:仅显示重复行;
-u:仅显示出现一次的行;
示例1:uniq命令去重,只能去连续的重复,如果隔开了还是会出现。

[ root@localhost ~]# cat fff. txt
linux 10
linux 20
linux 20       #linux10重复了2次
linux 30       #linux20重复了4次
linux 30       #linux30重复了2次
linux 10
linux 20
linux 20
centos 9
centos 6       #centos5重复了2次
centos 6       #centos6重复了2次
centos 7       #centos7重复了1次
centos 8       #centos8重复了2次
centos 8       
centos 5
[ root@localhost ~]# uniq fff . txt
linux 10
linux 20            #通过uniq命令去重,比较局限,只能去掉连续的重复,不能去隔开的重复
linux 30
linux 10
linux 20 
centos 5
centos 6
centos 7
centos 8
centos 5
[root@localhost ~]# uniq -u abc. txt
linux 10
linux 10
centos 5
centos 7
centos 5
[root@localhost ~]# uniq -d abc. txt
linux 20
1 inux 30
linux 20
centos 6
centos 8
[root@localhost ~]# uniq -c abc. txt
1 linux 10
2 linux 20
2 linux30
1 linux 10
2 linux 20
1 centos 5
2 centos 6
1 centos 7
2 centos 8
1 centos 5
[ root@localhost ~]#

如果我们想去掉所有的重复行,都只出现一次的话,可以使用 sort -u 命令

[ root@localhost ~]# sort -u abc.txt
centos 5
centos 6
centos 7    #所有重复的行只出现一次
centos 8
| linux 10
1 inux 20
| linux 30
| [ root@localhost ~]# uniq -u abc.txt
linux 10
linux 10 
centos 5   #将连续重复的行全部去掉
centos 7
centos 5
发布了69 篇原创文章 · 获赞 32 · 访问量 3447

猜你喜欢

转载自blog.csdn.net/qq_28361541/article/details/103452608