943,987 Members | Top Members by Rank

Ad:
Nov 5th, 2008
0

adding up some values

Expand Post »
Hi guys,

i have a ksh script that gets some info on processes and puts it into a temp file:

Shell Scripting Syntax (Toggle Plain Text)
  1. /usr/ucb/ps -auxxx|awk '{print $3," "$4," "$2," "$1," "$11}'|grep -v 0.0|sed 1d > $TMP

It gets the following data
Shell Scripting Syntax (Toggle Plain Text)
  1. 0.8 6.2158230724046144 9918 oracle
  2. 0.3 6.2158200964043168 15298 oracle
  3. 0.2 6.2158242644046904 5625 oracle
  4. 0.2 2.0158312081322712 17654 oracle
  5. 0.1 6.2158201444043200 15290 oracle
  6. 0.1 6.2158201364043200 15296 oracle
  7. 0.1 6.2158224884045592 16852 oracle
  8. 0.1 6.2158234244046464 16132 oracle
  9. 0.1 6.2158240724046880 28809 oracle
  10. 0.1 6.2158201444043200 15292 oracle
  11. 0.1 6.2158197764042832 17330 oracle
  12. 0.1 6.2158203764043512 17342 oracle
  13. 0.1 6.2158238164046720 7273 oracle
  14. 0.1 6.2158195364042576 17336 oracle
  15. 0.1 6.2158228324045632 17006 oracle
  16. 0.1 6.2158230644046328 5610 oracle
  17. 0.1 6.2158197924042872 17209 oracle
  18. 0.1 6.2158205844043728 16499 oracle
  19. 0.1 2.0158294401314312 17648 oracle
  20. 0.1 6.2158212564044408 9284 oracle
  21. 0.1 6.2158237364046520 16843 oracle
  22. 0.1 6.2158196964042760 17111 oracle
  23. 0.1 2.1158335361326672 17646 oracle
  24. 0.1 6.2158209684044176 10763 oracle
  25. 0.1 6.2158230084046232 12294 oracle
  26. 0.1 2.0158294401318936 17650 oracle
  27. 0.1 2.0158293761314280 17652 oracle
  28. 0.1 6.2158209444044152 15352 oracle
  29. 0.1 6.2158221524045120 16940 oracle
  30. 0.1 6.2158197924042824 17364 oracle
  31. 0.1 6.2158228564046016 16417 oracle
  32. 0.1 6.2158211124044288 22806 oracle
  33. 0.1 6.2158228484045920 3866 oracle
  34. 0.1 0.12219214296 11547 root 3628800
  35. 0.1 6.2158238564046736 11902 oracle
  36. 0.1 6.2158198484042896 17285 oracle
  37. 0.1 6.2158224164045592 17346 oracle
  38. 0.1 6.2158197924042888 17360 oracle
  39. 0.1 6.2158195684042608 17386 oracle
  40. 0.1 6.2158196884042752 17390 oracle

Im only really intersted in the first two columns and need the total of both columns.

Shell Scripting Syntax (Toggle Plain Text)
  1. TotCPU=`cat $TMP | awk '{print $1}' | nawk '{x+=$NF}END{print int x}'`
  2. #extract the total memory usage of all the processes
  3.  
  4. TotMem=`cat $TMP | awk '{print $2}' | nawk '{x+=$NF}END{print int x}'`
  5. #exptract total cpu usage of all currently running processes
  6. echo "total ps=$TotP,tot running ps=$RunP,total memory being used=$TotMem total cpu=$TotCPU" #debug

The echo produces:

total ps=1537,tot running ps=32,total memory being used=6167.813 total cpu=04.1

As well as some other lines but the one above is most important. Now the cpu one works apart from the annoyin leading 0 but the memory one produces a very wierd result. Any ideas?
Similar Threads
Reputation Points: 38
Solved Threads: 15
Posting Pro in Training
chris5126 is offline Offline
412 posts
since Feb 2006
Nov 5th, 2008
0

Re: adding up some values

Wow, that's a lot of pipes! I would do it something like this:

Shell Scripting Syntax (Toggle Plain Text)
  1. awk '{TotCPU += $1}{TotMem += $2}END{print "Total CPU= " TotCPU "\nTotal Mem= "TotMem}' test.list

Kinda ugly all in one line, but here it is broken down a little:

Shell Scripting Syntax (Toggle Plain Text)
  1. awk '\
  2. {TotCPU += $1}\
  3. {TotMem += $2}\
  4. END\
  5. {print "Total CPU= " TotCPU "\nTotal Mem= "TotMem}' test.list

Should output something like:

Shell Scripting Syntax (Toggle Plain Text)
  1. Total CPU= 5.1
  2. Total Mem= 221.639

Season (format) to taste!

-G
Reputation Points: 46
Solved Threads: 28
Junior Poster
Gromit is offline Offline
183 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: sed question
Next Thread in Shell Scripting Forum Timeline: problem running mysql shell script





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC