2017-06-11 73 views
0

如何在Linux Bash中使用pstophtop時將相同程序的進程組合在一起?通過命令合併Bash「ps」的行

例如,名爲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的值。

也許有另一個命令來實現這一目標?

+1

我建議看看'awk'。 – Cyrus

回答

1
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 
+0

謝謝。當我在之前的命令中輸入時,它會起作用。然而,這很麻煩,如果我想看看其他的表現統計數據,我不得不編輯很多東西。恥辱沒有選擇在ps或top或htop內執行這個看似平凡的任務。我可能不得不將它保存爲腳本。 – Tigran