Only matching each different pattern once

I have the following snippet of script:

for i in `ps axuc | awk '{ print $11 }' | grep -c 1 awk '{ print $1 }'` ; do
        ps axuc | grep $i | awk '{ do { x+=1 ; y+=$4 } while ( x < NR )} END { print y }'
done

The idea is to:

a) print the process list
b) grab the last field ($11 == command name)
c) execute addition for CPU utilization ($4 = %cpu key of ps) for each command name separately
d) print the result of addition

However, as there are many processes started with the same name (eg. httpd), it performs the addition redundantly for each instance. I presume the solution would be to modify the for-loop's preceding statement (assigning the field 11 of the output of ps to $i) to exclude the repeated occurences from being assigned to $i.

I guess the problem is rather simple, but I can't figure it out.

I tried the following:

for i in `ps axuc | awk '{ print $11 }' | grep -c 1 `ps axuc | awk '{ print $11 }'`` ; do [...]

First of all, of course; that is ridiculous. Second, it doesn't work:

syntax error: `|' unexpected

Anyone have any tips??

The solution lied commands "sort" and "uniq":

$ for i in `ps axuc | sed '1d' | awk '{ print $11 }' | sort | uniq` ; do sum=$(ps axuc | grep $i | awk '{ do { x+=1 ; y+=$4 } while ( x < NR )} END { print y }') ; print "$i\t" "$sum" ; done
cron 0.1
getty 0
httpd 3.3
inetd 0
init 0
irssi 0.4
ksh 0.4
mech 0.2
mountd 0
nano 0.6
nfsd 0
nmbd 0.2
ntpd 0.2
portmap 0.1
ps 0.1
screen 1
sftp-server 0.5
smbd 0
ssh 3
sshd 2.4
syslogd 0.2

Are you looking for something like this?

ps aux|awk 'NR>1{a[$11]+=$4}END{for(i in a)print i,a}'

Yes!

Thanks for the reply, your code is much cleaner & robust than what I came up with as a solution. Nice and compact.

Also, I didn't realize awk had arrays.

Thanks again!