Search code examples
linuxbashgrepps

How to list each process from ps aux, if they repeat, keep a running count and format


Right now, I use this:

ps aux | grep (Example_Process_1) -c

This returns:

221

This is nice, it gives me a count, but I have to do it for each of the 100 different items we have:

For instance,

ps aux | grep (Example_Process_1) -c
ps aux | grep (Example_Process_2) -c
ps aux | grep (Example_Process_3) -c

And so forth. What I want is a command to run ONCE, but the output is something similar:

Example Process 1 - 221
Example Process 2 - 360
Example Process 3 - 500

I realize I can write a script that will execute each of these, then I can output them to the screen, but it is large and clunky. Does anyone know any short clean code to accomplish this?


Solution

  • ps -e -o cmd | cut -f 1 -d ' '  | sort | uniq -c
    

    For all process, print the command line. Get the first word of the command line. Sort the list. Count occurrences of each.