sort 命令对 File 参数指定的文件中的行排序,并将结果写到标准输出。如果 File 参数指定多个文件,那么 sort 命令将这些文件连接起来,并当作一个文件进行排序。
sort 语法
sort [-fbMnrtuk] [file or stdin] 选项与参数: -f :忽略大小写的差异,例如 A 与 a 视为编码相同; -b :忽略最前面的空格符部分; -M :以月份的名字来排序,例如 JAN, DEC 等等的排序方法; -n :使用『纯数字』进行排序(默认是以文字型态来排序的); -r :逆序排序; -u :就是 uniq ,相同的数据中,仅出现一行代表; -t :分隔符,默认是用 [tab] 键来分隔; -k :以那个区间 (field) 来进行排序的意思
对/etc/passwd 的账号进行排序
smurf@Lenovo-U410:~$ cat /etc/passwd|sort avahi-autoipd:x:105:113:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false avahi:x:111:117:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false backup:x:34:34:backup:/var/backups:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin colord:x:113:121:colord colour management daemon,,,:/var/lib/colord:/bin/false daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin dnsmasq:x:104:65534:dnsmasq,,,:/var/lib/misc:/bin/false games:x:5:60:games:/usr/games:/usr/sbin/nologin gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin hplip:x:114:7:HPLIP system user,,,:/var/run/hplip:/bin/false irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin kernoops:x:106:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false libuuid:x:100:101::/var/lib/libuuid: lightdm:x:112:118:Light Display Manager:/var/lib/lightdm:/bin/false list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin messagebus:x:102:106::/var/run/dbus:/bin/false mysql:x:116:125:MySQL Server,,,:/nonexistent:/bin/false news:x:9:9:news:/var/spool/news:/usr/sbin/nologin nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin proxy:x:13:13:proxy:/bin:/usr/sbin/nologin pulse:x:115:122:PulseAudio daemon,,,:/var/run/pulse:/bin/false root:x:0:0:root:/root:/bin/bash rtkit:x:107:114:RealtimeKit,,,:/proc:/bin/false saned:x:108:115::/home/saned:/bin/false smurf:x:1000:1000:smurf,,,:/home/smurf:/bin/bash speech-dispatcher:x:110:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync syslog:x:101:104::/home/syslog:/bin/false sys:x:3:3:sys:/dev:/usr/sbin/nologin usbmux:x:103:46:usbmux daemon,,,:/home/usbmux:/bin/false uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin whoopsie:x:109:116::/nonexistent:/bin/false www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin sort 是默认以第一个数据来排序,而且默认是以字符串形式来排序,所以由字母 a 开始升序排序。
/etc/passwd 内容是以 : 来分隔的,我想以第三栏来排序,该如何
smurf@Lenovo-U410:~$ cat /etc/passwd | sort -t ':' -k 3 root:x:0:0:root:/root:/bin/bash smurf:x:1000:1000:smurf,,,:/home/smurf:/bin/bash libuuid:x:100:101::/var/lib/libuuid: uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin syslog:x:101:104::/home/syslog:/bin/false messagebus:x:102:106::/var/run/dbus:/bin/false usbmux:x:103:46:usbmux daemon,,,:/home/usbmux:/bin/false dnsmasq:x:104:65534:dnsmasq,,,:/var/lib/misc:/bin/false avahi-autoipd:x:105:113:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false kernoops:x:106:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false rtkit:x:107:114:RealtimeKit,,,:/proc:/bin/false saned:x:108:115::/home/saned:/bin/false whoopsie:x:109:116::/nonexistent:/bin/false speech-dispatcher:x:110:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh avahi:x:111:117:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false lightdm:x:112:118:Light Display Manager:/var/lib/lightdm:/bin/false colord:x:113:121:colord colour management daemon,,,:/var/lib/colord:/bin/false hplip:x:114:7:HPLIP system user,,,:/var/run/hplip:/bin/false pulse:x:115:122:PulseAudio daemon,,,:/var/run/pulse:/bin/false mysql:x:116:125:MySQL Server,,,:/nonexistent:/bin/false daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin proxy:x:13:13:proxy:/bin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin backup:x:34:34:backup:/var/backups:/usr/sbin/nologin list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
默认是以字符串来排序的,如果想要使用数字排序:
smurf@Lenovo-U410:~$ cat /etc/passwd|sort -t ":" -k 3n root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin proxy:x:13:13:proxy:/bin:/usr/sbin/nologin www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin backup:x:34:34:backup:/var/backups:/usr/sbin/nologin list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin libuuid:x:100:101::/var/lib/libuuid: syslog:x:101:104::/home/syslog:/bin/false messagebus:x:102:106::/var/run/dbus:/bin/false usbmux:x:103:46:usbmux daemon,,,:/home/usbmux:/bin/false dnsmasq:x:104:65534:dnsmasq,,,:/var/lib/misc:/bin/false avahi-autoipd:x:105:113:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false kernoops:x:106:65534:Kernel Oops Tracking Daemon,,,:/:/bin/false rtkit:x:107:114:RealtimeKit,,,:/proc:/bin/false saned:x:108:115::/home/saned:/bin/false whoopsie:x:109:116::/nonexistent:/bin/false speech-dispatcher:x:110:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/sh avahi:x:111:117:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false lightdm:x:112:118:Light Display Manager:/var/lib/lightdm:/bin/false colord:x:113:121:colord colour management daemon,,,:/var/lib/colord:/bin/false hplip:x:114:7:HPLIP system user,,,:/var/run/hplip:/bin/false pulse:x:115:122:PulseAudio daemon,,,:/var/run/pulse:/bin/false mysql:x:116:125:MySQL Server,,,:/nonexistent:/bin/false smurf:x:1000:1000:smurf,,,:/home/smurf:/bin/bash nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
转载于:https://blog.51cto.com/jin771998569/1657278