sort and uniq: sorting tools for Shell programming

Keywords: Linux ftp network DBus ssh

This article mainly writes some shell script sorting tools.

sort

Summary

Sort is a tool to sort the contents of files in behavioral units, or according to different data types.

usage

  • sort parameter

-f: ignore case
-b: ignore the space before each line
-M: sort by month
-n: sort by number
-r: reverse sort
-u: equivalent to uniq, indicating that only one row of the same data is displayed
-t: Specifies the separator, which is Tab separated by default
-O < output file >: transfer the sorted results to the specified file
-k: specify sorting area

Example

  • Sort the accounts in the / etc/passwd file
[root@localhost ~]# sort /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
  • Reverse order column 3 in the / etc/passwd file
[root@localhost ~]# sort -t ':' -rk 3 /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/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
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
  • Sort column 3 in the / etc/passwd file and save the output to user.txt
[root@localhost ~]# sort -t ':' -k 3 /etc/passwd -o user.txt
[root@localhost ~]# cat user.txt 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/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
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin

uniq

Summary

The uniq tool is often used in conjunction with the sort command to report or ignore duplicate lines in a file.

usage

  • uniq parameter

-c: count
-d: display only duplicate lines
-u: show only rows that appear once

Example

  • Delete duplicate lines in test.txt file
[root@localhost ~]# cat test.txt 
centos5
centos5
centos5
centos6
centos5
centos5
centos7
centos8
centos8
centos8
[root@localhost ~]# uniq test.txt 
centos5
centos6
centos5
centos7
centos8
  • Delete the duplicate lines in the test.txt file, and count the number of times to change the lines.
[root@localhost ~]# uniq -c test.txt
      3 centos5
      1 centos6
      2 centos5
      1 centos7
      3 centos8
  • Find duplicate lines in the test.txt file
[root@localhost ~]# uniq -d test.txt
centos5
centos5
centos8
  • Find the line that only appears once in the test.txt file
[root@localhost ~]# uniq -u test.txt
centos6
centos7

Posted by Bluemercury on Wed, 16 Oct 2019 14:01:17 -0700