Sort
The sort command sorts the rows in the file specified by the file parameter and writes the result to the standard output. If the file parameter specifies multiple files, the sort command connects these files and sorts them as one file.
Sort syntax
[Root @ WWW ~] # Sort [-fbmnrtuk] [file or stdin] Option and parameter:-F: Ignore case differences. For example, a and a are considered to be the same encoding;-B: ignore the leading space character.-M: sort by month name, such as Jan, Dec, and so on.-N: use "Numbers" for sorting (by default, it is sorted by text type);-R: reverse sorting;-u: uniq. In the same data, only one row is displayed;-T: delimiter, which is separated by the [Tab] key by default;-K: the meaning of sorting by field
Sort/etc/passwd accounts
[root@www ~]# cat /etc/passwd | sortadm:x:3:4:adm:/var/adm:/sbin/nologinapache:x:48:48:Apache:/var/www:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin
By default, sort is sorted by the first data, and is sorted by string by default.
The/etc/passwd content is separated by:. I want to sort it in the third column.
[root@www ~]# cat /etc/passwd | sort -t ':' -k 3root:x:0:0:root:/root:/bin/bashuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologin
It is sorted by string by default. If you want to sort by number:
cat /etc/passwd | sort -t ':' -k 3nroot:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/bin/shbin:x:2:2:bin:/bin:/bin/sh
The default value is ascending. If you want to sort in descending order
cat /etc/passwd | sort -t ':' -k 3nrnobody:x:65534:65534:nobody:/nonexistent:/bin/shntp:x:106:113::/home/ntp:/bin/falsemessagebus:x:105:109::/var/run/dbus:/bin/falsesshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin
If you want to forward/etc/passwd, sort the data by forward from 2nd to 4th characters in the sixth domain, and then sort the data by reverse direction based on the first domain.
cat /etc/passwd | sort -t':' -k 6.2,6.4 -k 1r sync:x:4:65534:sync:/bin:/bin/syncproxy:x:13:13:proxy:/bin:/bin/shbin:x:2:2:bin:/bin:/bin/shsys:x:3:3:sys:/dev:/bin/sh
Check the number of shells in/etc/passwd: sort the seventh domain of/etc/passwd, and then deduplicate:
cat /etc/passwd | sort -t':' -k 7 -uroot:x:0:0:root:/root:/bin/bashsyslog:x:101:102::/home/syslog:/bin/falsedaemon:x:1:1:daemon:/usr/sbin:/bin/shsync:x:4:65534:sync:/bin:/bin/syncsshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin