awk is my favorite tool for quick column addition.
for example, to sum the first column
Code:
cat file | awk '{SUM += $1} END {print SUM}'
This came in handy when I had a file with too many lines for use in open office calc.
Another thing I was able to do was filter some lines and sum the remaining on a file that listed user CPU utilization.
example:
Quote:
user1 331.2
user5 1000.8
user1 603.8
user2 1131.1
user4 3320.3
user4 3015.1
user2 1118.5
.
.
.
|
I could get user1's utilization this way.
Code:
cat file | grep user1 | awk '{SUM += $2} END {print SUM/3600}'
I divide the sum by 3600 because the numbers given were in seconds and I wanted the result time in hours. (awk even did the math for me!)
It was a trivial csh foreach statement to the get the number for all users.
Code:
foreach U ( `cat file | awk '{print $1}' | sort | uniq` )
cat file | grep $U | awk '{SUM += $2} END {print SUM/3600}'
end
I didn't even have to know beforehand which users were listed in the file.