服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - Linux命令 sort、uniq、tr工具详解

Linux命令 sort、uniq、tr工具详解

2020-07-28 18:15Ora. Linux

这篇文章主要介绍了Linux命令 sort、uniq、tr工具详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

Sort工具

Linux sort命令用于将文本文件内容加以排序。
sort可针对文本文件的内容,以行为单位来排序。

sort 常见选项

b 忽略每行前面开始出的空格字符。
-c 检查文件是否已经按照顺序排序。
-d 排序时,处理英文字母、数字及空格字符外,忽略其他的字符。
-f 排序时,将小写字母视为大写字母。
-i 排序时,除了040至176之间的ASCII字符外,忽略其他的字符。
-m 将几个排序好的文件进行合并。
-M 将前面3个字母依照月份的缩写进行排序。
-n 依照数值的大小排序。
-u 意味着是唯一的(unique),输出的结果是去完重了的。
-o<输出文件> 将排序后的结果存入指定的文件。
-r 以相反的顺序来排序。
-t<分隔字符> 指定排序时所用的栏位分隔字符。
+<起始栏位>-<结束栏位> 以指定的栏位来排序,范围由起始栏位到结束栏位的前一栏位。
–help 显示帮助。
–version 显示版本信息

sort工具示例

默认情况

默认情况下,sort工具会按字母顺序进行排序

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@1centos ~]# 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:/usr/share/httpd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
chrony:x:992:987::/var/lib/chrony:/sbin/nologin
cockpit-ws:x:990:984:User for cockpit-ws:/:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
dirsrv:x:988:982:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
dovenull:x:981:975:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin

将 /etc/passwd 以第三列进行反向排序

这边就以数字倒叙进行排序了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@1centos ~]# sort -t: -rk 3 /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
ods:x:999:999:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
polkitd:x:998:997:User for polkitd:/:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
unbound:x:996:993:Unbound DNS resolver:/etc/unbound:/sbin/nologin
gluster:x:995:992:GlusterFS daemons:/run/gluster:/sbin/nologin
libstoragemgmt:x:994:991:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
saslauth:x:993:76:Saslauthd user:/run/saslauthd:/sbin/nologin
chrony:x:992:987::/var/lib/chrony:/sbin/nologin
geoclue:x:991:985:User for geoclue:/var/lib/geoclue:/sbin/nologin
cockpit-ws:x:990:984:User for cockpit-ws:/:/sbin/nologin
sssd:x:989:983:User for sssd:/:/sbin/nologin
dirsrv:x:988:982:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
setroubleshoot:x:987:981::/var/lib/setroubleshoot:/sbin/nologin
saned:x:986:980:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
gnome-initial-setup:x:985:979::/run/gnome-initial-setup/:/sbin/nologin
pcp:x:984:978:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
kdcproxy:x:983:977:IPA KDC Proxy User:/:/sbin/nologin
ipaapi:x:982:976:IPA Framework User:/:/sbin/nologin
dovenull:x:981:975:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin
dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin
hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologi
……省略……

对 /etc/passwd 的第三列进行排序,输出到 px.txt 中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@1centos ~]# sort -t: -k 3 /etc/passwd -o px.txt
[root@1centos ~]# cat px.txt
root:x:0:0:root:/root:/bin/bash
xnftp:x:1007:1007::/home/xnftp:/sbin/nologin
vuser:x:1008:1008::/opt/vuser:/sbin/nologin
tom:x:1009:1009::/home/tom:/bin/bash
jerry:x:1010:1010::/home/jerry:/bin/bash
kongkong:x:1011:1011::/home/kongkong:/bin/bash
qemu:x:107:107:qemu user:/:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

uniq工具

Linux uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。

uniq常见选项

uniq 可检查文本文件中重复出现的行列
-c或–count 在每列旁边显示该行重复出现的次数。
-d或–repeated 仅显示重复出现的行列。
-f<栏位>或–skip-fields=<栏位> 忽略比较指定的栏位。
-s<字符位置>或–skip-chars=<字符位置> 忽略比较指定的字符。
-u或–unique 仅显示出一次的行列。
-w<字符位置>或–check-chars=<字符位置> 指定要比较的字符。
–help 显示帮助。
–version 显示版本信息。
[输入文件] 指定已排序好的文本文件。如果不指定此项,则从标准读取数据;
[输出文件] 指定输出的文件。如果不指定此选项,则将内容显示到标准输出设备(显示终端)

uniq工具示例

查看文件 the.txt

?
1
2
3
4
5
6
7
8
9
10
11
[root@1centos zhengzebiaodashi]# cat the.txt
1the 2the 3the
1the 2the 3the
1the 2the 3the
1the 2the 3the
 
1the 2the 3the
2the 2the 3the
3the 2the 3the 4the 5the
4hello hi the word world
5 2 3 4 5 6 7 8

默认排序

?
1
2
3
4
5
6
7
8
[root@1centos zhengzebiaodashi]# uniq the.txt
1the 2the 3the
 
1the 2the 3the
2the 2the 3the
3the 2the 3the 4the 5the
4hello hi the word world
5 2 3 4 5 6 7 8

删除重复行,并在显示重复出现的次数

?
1
2
3
4
5
6
7
8
[root@1centos zhengzebiaodashi]# uniq -c the.txt
   4 1the 2the 3the
   2
   1 1the 2the 3the
   1 2the 2the 3the
   1 3the 2the 3the 4the 5the
   1 4hello hi the word world
   1 5 2 3 4 5 6 7 8

查找 testfile 文件中的重复行

?
1
2
[root@1centos zhengzebiaodashi]# uniq -d the.txt
1the 2the 3the

tr工具

tr是translate的缩写,是用来做翻译或转换工作的。具体来讲,它可以对输入内容(stdin)进行转换或者删除。是linux管道的必备工具。下面来看几个常见的用法

tr常见选项

-c:取代所有不属于第一字符集的字符;
-d:删除所有属于第一字符集的字符;
-s:把连续重复的字符以单独一个字符表示;
-t:先删除第一字符集较第二字符集多出的字符

tr工具示例配合 echo 将展示的小写字母变大写

?
1
2
[root@1centos zhengzebiaodashi]# echo "jb51" |tr 'a-z' 'A-Z'
JB51

替换输出的重复字符

?
1
2
[root@1centos zhengzebiaodashi]# echo 'Thisssssss is cdsnnn' |tr -s 'sn'
This is cdsn

删除字符串中的默写字符

?
1
2
[root@1centos zhengzebiaodashi]# echo 'this is csdn' |tr -d 'th'
is is csdn

数组排序

有了这些工具,就可以简单的给数组进行升序或者降序了

?
1
2
3
4
5
6
7
8
9
10
#!/bin/bash
read -p "请输入你的数组,是空格隔开:" a
shuzu=($a)
echo "你的数组为:${shuzu[*]}"
echo "数组升序为:"
echo "$a" |tr ' ' '\n' |sort -n |tr '\n' ' '
echo ''
echo "数组降序为:"
echo "$a" |tr ' ' '\n' |sort -nr |tr '\n' ' '
echo " "

进行使用

?
1
2
3
4
5
6
7
[root@1centos zhengzebiaodashi]# source paixu.sh
请输入你的数组,是空格隔开:8 1 5 9 7
你的数组为:8 1 5 9 7
数组升序为:
1 5 7 8 9
数组降序为:
9 8 7 5 1

到此这篇关于Linux命令 sort、uniq、tr工具详解的文章就介绍到这了,更多相关Linux命令 sort、uniq、tr工具内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/Ora_G/article/details/107603908

延伸 · 阅读

精彩推荐