sort 概述
sort 是一个以行为单位对文件内容进行排序的工具, 也可以根据不同的数据类型来排序. 例如数据和字符的牌局就不一样. sort 命令的语法为 "sort [选项] 参数", 其中常用的选项包括以下几种.
-f: 忽略大小写;
-b: 忽略每行前面的空格;
-M: 按照月份进行排序;
-n: 按照数字进行排序;
-r: 反向排序;
-u: 等同于 uniq, 表示相同的数据仅显示一行;
-t: 指定分隔符, 默认使用 [Tab] 键分隔;
-o <输出文件>: 将排序后的结果转存至指定文件;
-k: 指定排序区域.
示例 1: 将 / etc/passwd 文件中的账号进行排序
- [[email protected] ~]# sort /etc/passwd
- abrt:x:173:173::/etc/abrt:/sbin/nologin
- adm:x:3:4:adm:/var/adm:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin
- bin:x:1:1:bin:/bin:/sbin/nologin
- ...
示例 2: 将 / etc/passwd 文件中第三列进行反向排序.
- [[email protected] ~]# sort -t ':' -rk 3 /etc/passwd
- nobody:x:99:99:Nobody:/:/sbin/nologin
- postfix:x:89:89::/var/spool/postfix:/sbin/nologin
- dbus:x:81:81:System message bus:/:/sbin/nologin
- mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
示例 3: 将 / etc/passwd 文件中第三列进行排序, 并将输出内容保存至 user.txt 文件中.
- [[email protected] ~]# sort -t ':' -k 3 /etc/passwd -o user.txt
- [[email protected] ~]# cat user.txt
- root:x:0:0:root:/root:/bin/bash
- uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
- operator:x:11:0:operator:/root:/sbin/nologin
uniq 工具概述
Uniq 工具在 Linux 系统中通常与 sort 命令结合使用, 用于报告或者忽略文件中的重复行. 具体的命令语法格式为: uniq [选项] 参数. 其中常用选项包括以下几种.
-c: 进行计数;
-d: 仅显示重复行;
-u: 仅显示出现一次的行;
示例 1: 删除 testfile 文件中的重复行.
- [[email protected] ~]# cat testfile Linux 10
- Linux 20
- Linux 30
- Linux 30
- Linux 30
- CentOS 6.5
- CentOS 6.5
- CentOS 6.5
- CentOS 7.3
- CentOS 7.3
- CentOS 7.3
- [[email protected] ~]# uniq testfile Linux 10
- Linux 20
- Linux 30
- CentOS 6.5
- CentOS 7.3
示例 2: 删除 testfile 文件中的重复行, 并在行首显示该行重复出现的次数.
- [[email protected] ~]# uniq -c testfile 1 Linux 10
- 1 Linux 20
- 3 Linux 30
- 3 CentOS 6.5
- 3 CentOS 7.3
示例 3: 查找 testfile 文件中的重复行.
- [[email protected] ~]# uniq -d testfile Linux 30
- CentOS 6.5
- CentOS 7.3
sort 可以把不连续的去重
uniq 只能去重连续的
谢谢阅读!!!
来源: http://www.bubuko.com/infodetail-3234208.html