zoukankan      html  css  js  c++  java
  • Linux命令输出头(标题)、输出结果排序技巧

     

    原文: http://blog.csdn.net/hongweigg/article/details/65446007

    --------------------------------------------------------

    1、 Linux命令输出头(标题)

    在使用Linux命令时,如果命令中有管道“|”,则输出的信息中,头(标题)信息丢失,要想看每一列代表什么意思很不方便。

    这里有一个简单的办法,通过2条命令叠加,获取头和内容。例如ps auxw:

    $ ps axuw
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root         1  0.0  0.2  37888  5952 ?        Ss   01:21   0:02 /sbin/init noprompt
    root         2  0.0  0.0      0     0 ?        S    01:21   0:00 [kthreadd]
    root         3  0.0  0.0      0     0 ?        S    01:21   0:00 [ksoftirqd/0]
    root         5  0.0  0.0      0     0 ?        S<   01:21   0:00 [kworker/0:0H]

    再加上管道符后

    $ ps axuw | grep java
    faster    8502  0.0  0.0  12948   972 pts/1    S+   02:15   0:00 grep java

    可以看到头(标题)已经丢失。

    怎么显示标题呢,这有一个简单的办法:

    $ ps axuw | head -1;ps axuw | grep java
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    faster    8510  0.0  0.0  12948   940 pts/1    S+   02:17   0:00 grep java

    也就是先用命令本身加“| head -1”取到头(标题),然后再使用该命令输出内容,两者叠加输出即得到所要结果。

    2、输出结果排序

    按列排序,数字大的在前:

    root@ubuntu:/home/faster/Fastdfs/FastDFS# ps auxw | sort -rn -k6
    root       851  0.2  1.6 408816 33224 ?        Ssl  01:21   0:10 /usr/bin/docker daemon -H fd://
    root       868  0.0  0.6 213068 13320 ?        Ssl  01:21   0:02 containerd -l /var/run/docker/libcontainerd/docker-containerd.sock --runtime runc --start-timeout 2m
    root      8452  0.0  0.3  95584  7212 ?        Ss   01:47   0:00 sshd: tiger [priv]
    root       980  0.0  0.3  95464  7088 ?        Ss   01:21   0:00 sshd: tiger [priv]
    root      1044  0.0  0.3  95464  7048 ?        Ss   01:23   0:00 sshd: tiger [priv]
    root       854  0.0  0.3  65612  6616 ?        Ss   01:21   0:00 /usr/sbin/sshd -D
    root       592  0.0  0.3 274592  6240 ?        Ssl  01:21   0:00 /usr/lib/accountsservice/accou

    该例子,将第6列进行排序,最大的数排前面。

    若只想看前10条的内容:

    ps auxw | sort -rn -k6 | head -10

    3、综合例子

    将实际内存消耗最大的10个进程显示出来的命令:

    ps auxw|head -1;ps auxw|sort -rn -k6|head -10

    $ ps auxw|head -1;ps auxw|sort -rn -k6|head -10
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root       851  0.2  1.6 408816 33224 ?        Ssl  01:21   0:11 /usr/bin/docker daemon -H fd://
    root       868  0.0  0.6 213068 13320 ?        Ssl  01:21   0:03 containerd -l /var/run/docker/libcontainerd/docker-containerd.sock --runtime runc --start-timeout 2m
    root      8452  0.0  0.3  95584  7212 ?        Ss   01:47   0:00 sshd: tiger [priv]
    root       980  0.0  0.3  95464  7088 ?        Ss   01:21   0:00 sshd: tiger [priv]
    root      1044  0.0  0.3  95464  7048 ?        Ss   01:23   0:00 sshd: tiger [priv]
    root       854  0.0  0.3  65612  6616 ?        Ss   01:21   0:00 /usr/sbin/sshd -D
    root       592  0.0  0.3 274592  6240 ?        Ssl  01:21   0:00 /usr/lib/accountsservice/accounts-daemon
    root         1  0.0  0.2  37888  5952 ?        Ss   01:21   0:02 /sbin/init noprompt
    syslog     576  0.0  0.2 256396  5372 ?        Ssl  01:21   0:00 /usr/sbin/rsyslogd -n
    faster    1137  0.0  0.2  21224  5272 pts/0    S    01:37   0:00 -su

    该命令亦可使用ps auxw --sort=-rss|head -10命令替代。

  • 相关阅读:
    HDU1074 Doing Homework(状压dp)
    HDU1069 Monkey and Banana(dp)
    HDU1024 Max Sum Plus Plus(dp)
    codeforces 1288E. Messenger Simulator(树状数组)
    codeforces 1288D. Minimax Problem(二分)
    geotrellis使用(三十二)大量GeoTiff文件实时发布TMS服务
    geotrellis使用(三十一)使用geotrellis直接将GeoTiff发布为TMS服务
    OpenStack(企业私有云)万里长征第六步——OpenStack网络及虚拟机存储位置
    geotrellis使用(三十)使用geotrellis读取PostGIS空间数据
    Docker Swarm——集群管理
  • 原文地址:https://www.cnblogs.com/oxspirt/p/8196088.html
Copyright © 2011-2022 走看看