Linux常见操作-将内容排序后输出

        排序需要使用到  sort
  

sort

        -r, --reverse:  降序,不指定默认是升序
        -h, ---human-numeric-sort:  以人类可读方式展示  (如:  2K  1G)
        -M, --month-sort:  可比较月份,(如  'JAN'  <  ...  <  'DEC')
        -k, --key=开始列号[,结束列号]:  指定按第几个字段排序,不指定默认是按整行排序,默认分隔符是空格
                若要指定分隔符如  ";",  则需要使用  -t
        -t, --field-separator=分隔符:  配合  -k  使用,指定分隔符,  不指定默认是空格
  
  

将  ps  -ef  内容进行排序输出

  
        ps -ef | sort -k3,3 -hr  默认分隔符为空格,按第3个字段降序排序

walker@LAPTOP-IHP7E9GG:~/new_interview$ sudo ps -ef | sort -k3,3 -hr
root      2624  2622  0 18:20 pts/1    00:00:00 ps -ef
mysql      845   718  0 03:26 ?        00:02:29 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --log-error=/var/log/mysql/error.log --pid-file=LAPTOP-IHP7E9GG.pid
walker    2623   553  0 18:20 pts/1    00:00:00 sort -k3,3 -hr
walker     947   553  0 03:57 pts/1    00:05:07 java -jar halo-latest.jar
walker     919   553  0 03:27 pts/1    00:00:10 python2.7 -m SimpleHTTPServer 8889
walker     635   553  0 03:20 pts/1    00:00:10 python2.7 -m SimpleHTTPServer 8888
root      2622   553  0 18:20 pts/1    00:00:00 sudo ps -ef
root       552   551  0 03:19 ?        00:00:02 /init
walker       9     8  0 Jan22 pts/0    00:00:00 -bash
UID        PID  PPID  C STIME TTY          TIME CMD

  
        ps -aux | sort -k6,6 -hr  默认分隔符为空格,按第6个字段(内存)降序排序

walker@LAPTOP-IHP7E9GG:~/new_interview$ sudo ps -aux | sort -k6,6 -hr
walker     947  0.5  6.1 8100424 796960 pts/1  Sl   03:57   5:08 java -jar halo-latest.jar
mysql      845  0.2  2.5 2081028 326544 ?      Sl   03:26   2:29 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --log-error=/var/log/mysql/error.log --pid-file=LAPTOP-IHP7E9GG.pid
walker     920  0.0  0.1  20548 13092 pts/1    S    03:27   0:10 python2.7 -m SimpleHTTPServer 8880
root      2633  0.0  0.0  10860  3392 pts/1    R+   18:26   0:00 ps -aux
mysql      718  0.0  0.0   2612  1744 ?        S    03:26   0:00 /bin/sh /usr/bin/mysqld_safe
root         1  0.0  0.0   1240   840 ?        Sl   Jan22   0:11 /init
walker    2632  0.0  0.0  23980   760 pts/1    S+   18:26   0:00 sort -k6,6 -hr
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

  

将  du  -sh  *  内容进行排序输出

  
        du -sh * | sort -rh  显示当前目录下文件和目录容量并按降序排列

walker@LAPTOP-IHP7E9GG:~/new_interview$ du -sh * | sort -rh
152M    adir
76M     halo-latest.jar
76M     adir2
76M     adir0
4.0K    test_sort.txt

  
          du -sh * | sort -rh | head -n 3  显示当前目录下文件和目录容量最高的3个

walker@LAPTOP-IHP7E9GG:~/new_interview$ du -sh * | sort -rh | head -n 3
152M    adir
76M     halo-latest.jar
76M     adir2

  
        ps -aux | grep python | sort -k6,6 -rh | head -n 5  查询最占内存的前5个python进程

walker@LAPTOP-IHP7E9GG:~/new_interview$ ps -aux | grep python | sort -k6,6 -rh | head -n 5
walker     635  0.0  0.1  20544 13276 pts/1    S    03:20   0:10 python2.7 -m SimpleHTTPServer 8888
walker     921  0.0  0.1  20548 13232 pts/1    S    03:27   0:10 python2.7 -m SimpleHTTPServer 8881
walker     923  0.0  0.1  20552 13188 pts/1    S    03:27   0:10 python2.7 -m SimpleHTTPServer 8883
walker     925  0.0  0.1  20548 13168 pts/1    S    03:27   0:10 python2.7 -m SimpleHTTPServer 8885
walker     922  0.0  0.1  20592 13168 pts/1    S    03:27   0:10 python2.7 -m SimpleHTTPServer 8882