Shell Programming sorting tool sort and uniq

CentOS Logo

Data in this chapter to write shellusing a script ordering tool.


sort

Outline

sortIs a tool in units of the contents of the file to be sorted, it can also be sorted according to different data types.

usage

  • sort [options] parameters

-f: Case Ignore
-b: Ignores the space in front of each line
-M: sorted by month
-n: sort numerically
-r: Reverse order
-u: equivalent to uniqrepresent the same data as the single line
-t: the specified delimiter, default Tabkey partition
-o <输出文件>: The sorted results dump file to specify
-k: a sorting area designated

Examples

  • The /etc/passwdfiles are sorted in the account
[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
  • The /etc/passwddocument first 3column reverse the sort order
[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
  • The /etc/passwddocument first 3column is sorted and saved to output content user.txtin
[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

Outline

uniqTools generally sortused in conjunction command, for reporting or ignore file duplicates.

usage

  • uniq [options] parameters

-c: Counting
-d: Display only duplicate rows
-u: row display appears only once

Examples

  • Delete test.txtfile duplicates
[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 test.txtfile duplicates and counts the number of repetitions diverted
[root@localhost ~]# uniq -c test.txt
      3 centos5
      1 centos6
      2 centos5
      1 centos7
      3 centos8
  • Find test.txtfile duplicates
[root@localhost ~]# uniq -d test.txt
centos5
centos5
centos8
  • Find test.txtthe file appears only once in the row
[root@localhost ~]# uniq -u test.txt
centos6
centos7

Guess you like

Origin www.cnblogs.com/llife/p/11682072.html