Sort
The sort command sorts the rows in the files specified by the file parameter and writes the results to standard output. If The file parameter specifies multiple files, the sort command connects the files and sorts them as a file.
Sort syntax
[[email protected] ~]# sort [-fbmnrtuk] [file or stdin]
Options and Parameters:
-F: Ignores case differences, such as a and a are treated as encodings;
-B: Ignores the first whitespace part;
-M: Sort by month name, such as JAN, DEC and so on;
-N: Sort using "pure number" ( by default it is sorted by text type );
-R: Reverse sort;
-U: is uniq , in the same data, only one row is represented;
-T: delimiter, the default is to use the [tab] key to separate;
-K: Sort by that range (field) to /etc/passwd 's account number
Specific examples:
[Email protected] ~]# CAT/ETC/PASSWD | Sort
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
Daemon:x:2:2:daemon:/sbin:/sbin/nologinsort
is sorted by default with the first data, and by default it is sorted in string form , so the letter a begins in ascending order.
/ETC/PASSWD content is separated by : , I want to sort by the third column, How to
[Email protected] ~]# CAT/ETC/PASSWD | Sort-t ': '-K 3
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
Bin:x:1:1:bin:/bin:/sbin/nologin
Games:x:12:100:games:/usr/games:/sbin/nologin
The default is to sort by string, if you want to use numbers to sort:
cat/etc/passwd | Sort-t ': '-K 3n
Root:x:0:0:root:/root:/bin/bash
Daemon:x:1:1:daemon:/usr/sbin:/bin/sh
Bin:x:2:2:bin:/bin:/bin/sh
The default is ascending sort, if you want to sort in reverse order, as follows
cat/etc/passwd | Sort-t ': '-K 3NR
Nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
Ntp:x:106:113::/home/ntp:/bin/false
Messagebus:x:105:109::/var/run/dbus:/bin/false
Sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin
If you are going to /etc/passwd, first sort by the first 2 characters of the sixth field to the 4 characters, and then reverse-sort based on the domain one.
cat/etc/passwd | Sort-t ': '-K 6.2,6.4-k 1r
Sync:x:4:65534:sync:/bin:/bin/sync
Proxy:x:13:13:proxy:/bin:/bin/sh
Bin:x:2:2:bin:/bin:/bin/sh
Sys:x:3:3:sys:/dev:/bin/sh
See how many shells the /etc/passwd has : sort the seventh field of /etc/passwd and then go back to the heavy one :
cat/etc/passwd | Sort-t ': '-K 7-u
Root:x:0:0:root:/root:/bin/bash
Syslog:x:101:102::/home/syslog:/bin/false
Daemon:x:1:1:daemon:/usr/sbin:/bin/sh
Sync:x:4:65534:sync:/bin:/bin/sync
Sshd:x:104:65534::/var/run/sshd:/usr/sbin/nologin
This article is from the "Tiandaochouqin" blog, make sure to keep this source http://8855546.blog.51cto.com/8845546/1641404
Sort command Usage