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

一、sort 工具

  • sort 是一个以行为单位对文件内容进行排序的工具,也可以根据不同的数据类型来排序
  • sort 命令格式:sort [选项] 参数
  • 常用的选项:
选项作用
-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 工具

  • uniq 工具在 Linux 系统中通常与 sort 命令结合使用,用于报告或者忽略文件中的重复行
  • 命令语法格式为:uniq [选项] 参数
  • 常用选项包括:
选项作用
-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
linux 20
linux 30
[ root@localhost ~]# uniq -u abc.txt
linux 10
linux 10 
centos 5   #将连续重复的行全部去掉
centos 7
centos 5
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值