Awk/Unix group by

前端 未结 6 1484
鱼传尺愫
鱼传尺愫 2020-12-04 14:17

have this text file:

name, age
joe,42
jim,20
bob,15
mike,24
mike,15
mike,54
bob,21

Trying to get this (count):

joe 1
jim 1
         


        
6条回答
  •  盖世英雄少女心
    2020-12-04 15:15

    I come up with two functions based on the answers here:

    topcpu() {
        top -b -n1                                                                                  \
            | tail -n +8                                                                            \
            | awk '{ print $12, $9, $10 }'                                                          \
            | awk '{ CPU[$1] += $2; MEM[$1] += $3 } END { for (k in CPU) print k, CPU[k], MEM[k] }' \
            | sort -k3 -n                                                                           \
            | tail -n 10                                                                            \
            | column -t                                                                             \
            | tac
    }
    
    topmem() {
        top -b -n1                                                                                  \
            | tail -n +8                                                                            \
            | awk '{ print $12, $9, $10 }'                                                          \
            | awk '{ CPU[$1] += $2; MEM[$1] += $3 } END { for (k in CPU) print k, CPU[k], MEM[k] }' \
            | sort -k2 -n                                                                           \
            | tail -n 10                                                                            \
            | column -t                                                                             \
            | tac
    }
    
    $ topcpu
    chrome           0    75.6
    gnome-shell      6.2  7
    mysqld           0    4.2
    zsh              0    2.2
    deluge-gtk       0    2.1
    Xorg             0    1.6
    scrcpy           0    1.6
    gnome-session-b  0    0.8
    systemd-journal  0    0.7
    ibus-x11         6.2  0.7
    
    $ topmem
    top              12.5  0
    Xorg             6.2   1.6
    ibus-x11         6.2   0.7
    gnome-shell      6.2   7
    chrome           6.2   74.6
    adb              6.2   0.1
    zsh              0     2.2
    xdg-permission-  0     0.2
    xdg-document-po  0     0.1
    xdg-desktop-por  0     0.4
    

    enjoy!

提交回复
热议问题