Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Only matching each different pattern once

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-29-2008   -   Original Discussion by fiori_musicali
fiori_musicali fiori_musicali is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 16 February 2009, 4:58 PM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Only matching each different pattern once

I have the following snippet of script:


Code:
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:


Code:
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??
Sponsored Links
    #2  
Old Unix and Linux 11-29-2008   -   Original Discussion by fiori_musicali
fiori_musicali fiori_musicali is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 16 February 2009, 4:58 PM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Bug

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

Sponsored Links
    #3  
Old Unix and Linux 11-29-2008   -   Original Discussion by fiori_musicali
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 14 October 2017, 3:52 AM EDT
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts
Are you looking for something like this?


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

    #4  
Old Unix and Linux 11-29-2008   -   Original Discussion by fiori_musicali
fiori_musicali fiori_musicali is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 16 February 2009, 4:58 PM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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!
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk - pattern matching? citizencro UNIX for Dummies Questions & Answers 4 05-08-2011 09:32 PM
counting the lines matching a pattern, in between two pattern, and generate a tab d.chauliac Shell Programming and Scripting 4 03-19-2009 02:30 PM
comment/delete a particular pattern starting from second line of the matching pattern imas Shell Programming and Scripting 4 10-13-2008 03:37 AM
pattern matching mercuryshipzz Shell Programming and Scripting 4 01-14-2008 11:01 PM
AWK pattern matching, first and last smb_uk Shell Programming and Scripting 10 12-27-2007 09:03 PM



All times are GMT -4. The time now is 10:20 PM.