在for循环中运行时,top命令显示额外的10行

时间:2019-04-05 14:37:47

标签: linux bash redhat

我运行命令

for cpu in `cat /proc/cpuinfo | grep processor | wc -l`; do
    top -b -c -n$cpu | egrep -v 'Mem|Swap|{top -b -c}' | grep load -A10 | grep -v grep
done

但是shell会额外打印10行,我希望在每次调用删除后最后10行。

这是完整的输出,我希望从每个段落中删除'-'之后的行

]# for cpu in `cat /proc/cpuinfo |grep processor |wc -l`;do top -b -c -n$cpu |egrep -v 'Mem|Swap|{top -b -c}' |grep load -A10 |grep -v grep; done
top - 07:34:27 up 17 days,  9:04,  1 user,  load average: 0.00, 0.02, 0.68
Tasks: 268 total,   1 running, 267 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.9%us,  0.1%sy,  0.0%ni, 98.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 4193 root      20   0 28164 1652 1136 R  2.0  0.0   0:00.01 top -b -c -n8
14303 appuser   20   0 12.6g 3.2g  56m S  2.0 10.2 180:45.23 /apps/jdk1.8.0_151/bin/java -D[Standalone] -XX:+UseCompressedOops -server -Xms2048m -Xmx6144m -XX:PermSize=256m -XX:MaxPermSize=512m -Djava.ne
    1 root      20   0 30068 1664 1444 S  0.0  0.0   0:01.54 /sbin/init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.02 [kthreadd]
    3 root      RT   0     0    0    0 S  0.0  0.0   0:00.39 [migration/0]
    4 root      20   0     0    0    0 S  0.0  0.0   0:02.43 [ksoftirqd/0]
--
 8629 daemon    20   0 56504 8412 5756 S  0.0  0.0   0:05.57 /opt/quest/sbin/.vasd -p /var/opt/quest/vas/vasd/.vasd.pid
 8630 daemon    20   0 56476 7724 5148 S  0.0  0.0   0:00.04 /opt/quest/sbin/.vasd -p /var/opt/quest/vas/vasd/.vasd.pid
 8631 daemon    20   0 54404 7436 4840 S  0.0  0.0   0:00.22 /opt/quest/sbin/.vasd -p /var/opt/quest/vas/vasd/.vasd.pid
13089 root      18  -2 10764  392  304 S  0.0  0.0   0:00.00 /sbin/udevd -d
13090 root      18  -2 10764  416  288 S  0.0  0.0   0:00.00 /sbin/udevd -d
13203 root      20   0  254m 8120 4944 S  0.0  0.0  12:02.50 /usr/sbin/vmtoolsd
13227 root      20   0 60060   9m 7280 S  0.0  0.0   0:00.06 /usr/lib/vmware-vgauth/VGAuthService -s
14259 root      20   0     0    0    0 S  0.0  0.0   0:13.53 [flush-253:6]
14262 appuser   20   0  103m 1456 1196 S  0.0  0.0   0:00.00 /bin/sh ./standalone-mdm-hub-server.sh -c standalone-full.xml -b 0.0.0.0 -bmanagement 0.0.0.0 -u 230.0.0.4 -Djboss.server.base.dir=../mdm-hub-
--
top - 07:34:30 up 17 days,  9:04,  1 user,  load average: 0.00, 0.02, 0.68
Tasks: 268 total,   1 running, 267 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.2%us,  0.1%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3581 root      20   0 2055m  51m  17m S  1.0  0.2 237:05.21 /usr/share/metricbeat/bin/metricbeat -c /etc/metricbeat/metricbeat.yml -path.home /usr/share/metricbeat -path.config /etc/metricbeat -path.dat
 4193 root      20   0 28172 1760 1236 R  0.7  0.0   0:00.03 top -b -c -n8
14303 appuser   20   0 12.6g 3.2g  56m S  0.7 10.2 180:45.25 /apps/jdk1.8.0_151/bin/java -D[Standalone] -XX:+UseCompressedOops -server -Xms2048m -Xmx6144m -XX:PermSize=256m -XX:MaxPermSize=512m -Djava.ne
   42 root      20   0     0    0    0 S  0.3  0.0   3:05.13 [events/7]
13203 root      20   0  254m 8120 4944 S  0.3  0.0  12:02.51 /usr/sbin/vmtoolsd
14553 appuser   20   0 22.5g  18g  54m S  0.3 57.3   1467:43 /apps/jdk1.8.0_151/bin/java -D[Standalone] -XX:+UseCompressedOops -server -Xms2048m -Xmx16000m -XX:PermSize=512m -XX:MaxPermSize=1048m -Djava.
--
 8629 daemon    20   0 56504 8412 5756 S  0.0  0.0   0:05.57 /opt/quest/sbin/.vasd -p /var/opt/quest/vas/vasd/.vasd.pid
 8630 daemon    20   0 56476 7724 5148 S  0.0  0.0   0:00.04 /opt/quest/sbin/.vasd -p /var/opt/quest/vas/vasd/.vasd.pid
 8631 daemon    20   0 54404 7436 4840 S  0.0  0.0   0:00.22 /opt/quest/sbin/.vasd -p /var/opt/quest/vas/vasd/.vasd.pid
13089 root      18  -2 10764  392  304 S  0.0  0.0   0:00.00 /sbin/udevd -d
13090 root      18  -2 10764  416  288 S  0.0  0.0   0:00.00 /sbin/udevd -d
13227 root      20   0 60060   9m 7280 S  0.0  0.0   0:00.06 /usr/lib/vmware-vgauth/VGAuthService -s
14259 root      20   0     0    0    0 S  0.0  0.0   0:13.53 [flush-253:6]
14262 appuser   20   0  103m 1456 1196 S  0.0  0.0   0:00.00 /bin/sh ./standalone-mdm-hub-server.sh -c standalone-full.xml -b 0.0.0.0 -bmanagement 0.0.0.0 -u 230.0.0.4 -Djboss.server.base.dir=../mdm-hub-
14512 appuser   20   0  103m 1452 1196 S  0.0  0.0   0:00.01 /bin/sh ./standalone-mdm-process-server.sh -c standalone-full.xml -b 0.0.0.0 -bmanagement 0.0.0.0 -Djboss.server.base.dir=../mdm-process-serve

1 个答案:

答案 0 :(得分:0)

因为找到“ grep加载”。总是花一点时间来分解您正在做的事情,并看看中间产品。试试:

top -b -c -n1 |egrep -v 'Mem|Swap|{top -b -c}' |grep load -A10

快速解决方案是将-m1仅添加到第一个匹配项中。