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
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 the name of the month, such as the sort method of JAN, DEC, and so on;-N : Use "pure numbers" to sort (by default, sort by text);-R: reverse order;-U: Uniq, the same data, only one row is represented;-T: delimiter, which is delimited by the [tab] key by default;-K: The meaning of sorting by that interval (field)
Sort the accounts of the/etc/passwd
[Email protected]:~$ cat /etc/passwd|sortavahi-autoipd:x:105:113:avahi autoip daemon, ,,:/var/lib/avahi-autoipd:/bin/falseavahi:x:111:117:avahi mdns daemon,,,:/var/run/avahi-daemon:/bin/ falsebackup:x:34:34:backup:/var/backups:/usr/sbin/nologinbin:x:2:2:bin:/bin:/usr/sbin/nologincolord:x:113:121: Colord colour management daemon,,,:/var/lib/colord:/bin/falsedaemon:x:1:1:daemon:/usr/sbin:/usr /SBIN/NOLOGINDNSMASQ:X:104:65534:DNSMASQ,,,:/var/lib/misc:/bin/falsegames:x:5:60:games:/usr/games:/usr/sbin/ nologingnats:x:41:41:gnats bug-reporting system (Admin):/var/lib/gnats:/usr/sbin/nologinhplip:x : 114:7:hplip system user,,,:/var/run/hplip:/bin/falseirc:x:39:39:ircd:/var/run/ircd:/usr/sbin/ Nologinkernoops:x:106:65534:kernel oops tracking daemon,,,:/:/bin/falselibuuid:x:100:101::/var/ Lib/libuuid:lightdm:x:112:118:light display manager:/var/lib/lightdm:/bin/falselist:x:38:38:mailing list manager:/var/list:/usr/sbin/nologinlp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologinmail:x:8:8:mail:/var/mail:/usr/ Sbin/nologinman:x:6:12:man:/var/cache/man:/usr/sbin/nologinmessagebus:x:102:106::/var/run/dbus:/bin/falsemysql : X:116:125:mysql server,,,:/nonexistent:/bin/falsenews:x:9:9:news:/var/spool/news:/usr/sbin/nologinnobody : x:65534:65534:nobody:/nonexistent:/usr/sbin/nologinproxy:x:13:13:proxy:/bin:/usr/sbin/nologinpulse:x:115:122: Pulseaudio daemon,,,:/var/run/pulse:/bin/falseroot:x:0:0:root:/root:/bin/bashrtkit:x:107:114:realtimekit, ,,:/proc:/bin/falsesaned:x:108:115::/home/saned:/bin/falsesmurf:x:1000:1000:smurf,,,:/home/smurf:/bin/ Bashspeech-dispatcher:x:110:29:speech dispatcher,,,:/var/run/speech-dispatcher:/bin/shsync:x:4:65534:sync :/bin:/bin/syncsyslog:x:101:104::/home/syslog:/bin/falsesys:x:3:3:sys:/dev:/usr/sbin/nologinusbmux:x:103:46: Usbmux daemon,,,:/home/usbmux:/bin/falseuucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologinwhoopsie:x : 109:116::/nonexistent:/bin/falsewww-data:x:33:33:www-data:/var/www:/usr/sbin/nologinsort is sorted by default with the first data, and is sorted by string, so the letter a Start sorting 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 3root : X:0:0:root:/root:/bin/bashsmurf:x:1000:1000:smurf,,,:/home/smurf:/bin/bashlibuuid:x:100:101::/var/lib/libuuid : uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologinsyslog:x:101:104::/home/syslog:/bin/falsemessagebus:x : 102:106::/var/run/dbus:/bin/falseusbmux:x:103:46:usbmux daemon,,,:/home/usbmux:/bin/falsednsmasq:x : 104:65534:dnsmasq,,,:/var/lib/misc:/bin/falseavahi-autoipd:x:105:113:avahi autoip daemon,,,:/var/ Lib/avahi-autoipd:/bin/falsekernoops:x:106:65534:kernel oops tracking daemon,,,:/:/bin/ Falsertkit:x:107:114:realtimekit,,,:/proc:/bin/falsesaned:x:108:115::/home/saned:/bin/falsewhoopsie:x:109:116: :/nonexistent:/bin/falsespeech-dispatcher:x:110:29:speech dispatcher,,,:/var/run/speech-dispatcher:/bin/ Shavahi:x:111:117:avahi mdns daemon,,,:/var/run/avahi-daemon:/bin/falselightdm:x:112:118:light Display manager:/var/lib/liGhtdm:/bin/falsecolord:x:113:121:colord colour management daemon,,,:/var/lib/colord:/bin/ Falsehplip:x:114:7:hplip system user,,,:/var/run/hplip:/bin/falsepulse:x:115:122:pulseaudio Daemon,,,:/var/run/pulse:/bin/falsemysql:x:116:125:mysql server,,,:/nonexistent:/bin/falsedaemon:x:1:1: daemon:/usr/sbin:/usr/sbin/nologinproxy:x:13:13:proxy:/bin:/usr/sbin/nologinbin:x:2:2:bin:/bin:/usr/sbin/ NOLOGINWWW-DATA:X:33:33:WWW-DATA:/VAR/WWW:/USR/SBIN/NOLOGINSYS:X:3:3:SYS:/DEV:/USR/SBIN/NOLOGINBACKUP:X:34:34: backup:/var/backups:/usr/sbin/nologinlist:x:38:38:mailing list manager:/var/list:/usr/sbin/ Nologinirc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologingnats:x:41:41:gnats bug-reporting system (Admin):/var/lib/gnats:/usr/sbin/nologinsync:x:4:65534:sync:/bin:/bin/syncgames:x:5:60:games:/usr/games :/USR/SBIN/NOLOGINMAN:X:6:12:MAN:/VAR/CACHE/MAN:/USR/SBIN/NOLOGINNOBODY:X:65534:65534:NOBODY:/NONEXISTENT:/USR /sbin/nologinlp:x:7:7:lp:/var/spool/lpd:/Usr/sbin/nologinmail:x:8:8:mail:/var/mail:/usr/sbin/nologinnews:x:9:9:news:/var/spool/news:/usr/sbin/nologin
The
is sorted by string by default, if you want to use numeric sorting:
[email protected]:~$ cat /etc/passwd|sort -t ":" -k 3nroot:x:0:0:root:/root :/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologinbin:x:2:2:bin:/bin:/usr/sbin/nologinsys:x:3:3:sys:/ Dev:/usr/sbin/nologinsync:x:4:65534:sync:/bin:/bin/syncgames:x:5:60:games:/usr/games:/usr/sbin/nologinman:x : 6:12:man:/var/cache/man:/usr/sbin/nologinlp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologinmail:x:8:8:mail:/var/ mail:/usr/sbin/nologinnews:x:9:9:news:/var/spool/news:/usr/sbin/nologinuucp:x:10:10:uucp:/var/spool/uucp:/usr/ sbin/nologinproxy:x:13:13:proxy:/bin:/usr/sbin/nologinwww-data:x:33:33:www-data:/var/www:/usr/sbin/ nologinbackup:x:34:34:backup:/var/backups:/usr/sbin/nologinlist:x:38:38:mailing list manager:/var/ List:/usr/sbin/nologinirc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologingnats:x:41:41:gnats bug-reporting System (Admin):/var/lib/gnats:/usr/sbin/nologinlibuuid:x:100:101::/var/lib/libuuid:syslog:x:101:104 ::/home/syslog:/bin/falsemessagebUs:x:102:106::/var/run/dbus:/bin/falseusbmux:x:103:46:usbmux daemon,,,:/home/usbmux:/bin/falsednsmasq:x : 104:65534:dnsmasq,,,:/var/lib/misc:/bin/falseavahi-autoipd:x:105:113:avahi autoip daemon,,,:/var/ Lib/avahi-autoipd:/bin/falsekernoops:x:106:65534:kernel oops tracking daemon,,,:/:/bin/ Falsertkit:x:107:114:realtimekit,,,:/proc:/bin/falsesaned:x:108:115::/home/saned:/bin/falsewhoopsie:x:109:116: :/nonexistent:/bin/falsespeech-dispatcher:x:110:29:speech dispatcher,,,:/var/run/speech-dispatcher:/bin/ Shavahi:x:111:117:avahi mdns daemon,,,:/var/run/avahi-daemon:/bin/falselightdm:x:112:118:light display manager:/var/lib/lightdm:/bin/falsecolord:x:113:121:colord colour management Daemon,,,:/var/lib/colord:/bin/falsehplip:x:114:7:hplip system user,,,:/var/run/hplip:/bin/ Falsepulse:x:115:122:pulseaudio daemon,,,:/var/run/pulse:/bin/falsemysql:x:116:125:mysql server,,,:/ Nonexistent:/bin/falsesmurf:x:1000:1000:smurf,,,:/home/smurf:/bin/bashnobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
This article is from the "Smurf Linux ops" blog, so be sure to keep this source http://jin771998569.blog.51cto.com/2147853/1657278
Sort command: Sorting files