[英]Combine rows of Bash “ps” by command
使用ps
, top
或htop
時,如何在Linux Bash中合並同一程序的htop
?
例如,當調用ps -eo pmem,pcpu,args
代替此命令時:
...
2.0 1.0 /usr/sbin/apache2 -k start
3.0 2.0 /usr/sbin/apache2 -k start
5.0 1.0 /usr/sbin/apache2 -k start
2.5 1.0 /usr/sbin/mysqld
...
它會顯示
...
10.0 4.0 /usr/sbin/apache2 -k start
2.5 1.0 /usr/sbin/mysqld
...
與內存和CPU值相加。
也許還有另一個命令可以實現這一目標?
awk '{m=$1; c=$2; $1=$2=""; pmem[$0]+=m; pcpu[$0]+=c} END{for(i in pmem) {printf("%5.1f %5.1f %s\n",pmem[i], pcpu[i], substr(i,3))}}' file
輸出:
10.0 4.0 /usr/sbin/apache2 -k start 2.5 1.0 /usr/sbin/mysqld
有一些評論:
awk '{m=$1; c=$2 # save column 1 and 2
$1=$2="" # remove content of columns 1 and 2
pmem[$0]+=m; pcpu[$0]+=c} # save memory and cpu to hashes and
# add to its value, use rest of
# row as key
# print content of both hashes and key in a loop
END{for(i in pmem) {printf("%5.1f %5.1f %s\n",pmem[i], pcpu[i], substr(i,3))}}' file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.