Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a script in ksh. I need to find all users who have more than N processes and echo them in the shell. N is read from ksh.

I know that I should use ps -elf, but how do I parse it, find users with >N processes, and create an array with them? I'm having a little trouble with arrays in ksh. Maybe a simple solution can help me instead of having to to create an array. One person recommended that I use

ps -elf | awk '{a[$3]++;}END{for(i in a)if (a[i]>N)print i, a[i];}' N=3

but it doesn't work correctly.

share|improve this question
up vote 11 down vote accepted

You should reduce the columns output by ps to the minimum, i.e. request only the username here - this simplifies further processing.

For example:

$ ps -eo user=

will print the owner of all the currently running processes (= suppresses the header).

An easy way to get the counts for each user:

$ ps -eo user= | sort | uniq -c
  1 dovecot
  1 messagebus
  1 mpd
  1 polkitd
  2 postfix
121 root
 27 me

You can then filter that as you wish:

$ ps -eo user= | sort | uniq -c | awk '$1 > 42 {print $2}'

With a limit in a variable:

$ N=42
$ ps -eo user= | sort | uniq -c | awk -v limit="$N" '$1 > limit {print $2}'
share|improve this answer
BEST WAY ! THX!! – Alex Zern Mar 19 '13 at 15:02
Thanks for the fixes @Stephane – Mat Mar 19 '13 at 15:50
+1 You can also sort the output by the numeric column descending by piping to sort -nrk 1 – l0b0 Mar 19 '13 at 16:08
@l0b0, sort -rn will do the same as sort -rnk1, since -n ignores the leading blanks anyway. – Stéphane Chazelas Mar 19 '13 at 16:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.