awk question about sum


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk question about sum
# 1  
Old 01-26-2012
Power awk question about sum

Hi everyone, i need help with a simple task. I have a file withe the format:


"01/20/2012 23:10:13.979","49","49","48","19"
"01/20/2012 23:15:13.969","47","47","48","18"
"01/20/2012 23:20:13.975","47","47","45","17"
"01/20/2012 23:25:13.980","44","44","44","17"
"01/20/2012 23:30:13.970","43","43","47","14"
"01/20/2012 23:35:13.976","45","45","44","15"
"01/20/2012 23:40:13.966","45","45","43","14"
"01/20/2012 23:45:13.971","42","42","44","14"
"01/20/2012 23:50:13.977","44","44","44","13"
"01/20/2012 23:55:13.966","44","44","42","12"

I need the next result:

"01/20/2012 23:10:13.979","49","49","48","19","165"
"01/20/2012 23:15:13.969","47","47","48","18","159"
"01/20/2012 23:20:13.975","47","47","45","17","156"
"01/20/2012 23:25:13.980","44","44","44","17","149"
"01/20/2012 23:30:13.970","43","43","47","14","147"
"01/20/2012 23:35:13.976","45","45","44","15","149"
"01/20/2012 23:40:13.966","45","45","43","14","147"
"01/20/2012 23:45:13.971","42","42","44","14","140"
"01/20/2012 23:50:13.977","44","44","44","13","145"
"01/20/2012 23:55:13.966","44","44","42","12","142"

Im neewbie with awk, i try with:

awk '
BEGIN {FS=OFS=","}
{
sum=0; n=0
for(k=2;k<=NF;k++)
{sum+=$k; ++n}
print $0, sum
}'file.txt > newfile.txt

But its just add a 0 at the end. Smilie

I'm stuck with this for a couple of hours, any idea? THX A LOT!!
# 2  
Old 01-26-2012
Is perl ok?
Code:
perl -F"," -lane 's/"//g for(@F);print $_.",\"",$F[1]+$F[2]+$F[3]+$F[4]."\""' input

These 2 Users Gave Thanks to balajesuri For This Post:
# 3  
Old 01-26-2012
Thx a lot balajesuri!!! It's works excellent!!
# 4  
Old 01-27-2012
Code:
 
$ nawk -F"[\",]" '{a=$5+$8+$11+$14;printf("%s,\"%s\"\n",$0,a)}' input.txt
"01/20/2012 23:10:13.979","49","49","48","19","165"
"01/20/2012 23:15:13.969","47","47","48","18","160"
"01/20/2012 23:20:13.975","47","47","45","17","156"
"01/20/2012 23:25:13.980","44","44","44","17","149"
"01/20/2012 23:30:13.970","43","43","47","14","147"
"01/20/2012 23:35:13.976","45","45","44","15","149"
"01/20/2012 23:40:13.966","45","45","43","14","147"
"01/20/2012 23:45:13.971","42","42","44","14","142"
"01/20/2012 23:50:13.977","44","44","44","13","145"
"01/20/2012 23:55:13.966","44","44","42","12","142"

This User Gave Thanks to itkamaraj For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk and sum

This is my file vol0 285GB vol0.snapshot 15GB vol11_root 0GB vol12_root 47GB vol12_root.snapshot 2GB I need the output vol0 285GB,vol0.snapshot 15GB,sum-300GB vol11_root 0GB,nosnap,sum-0Gb vol12_root 47GB,vol12_root.snapshot 2GB,49GB I was trying to use paste -d, --. But... (9 Replies)
Discussion started by: ranjancom2000
9 Replies

2. Shell Programming and Scripting

awk - Print Sum

Hi, I have an awk command that I am using, and part of it sums COL_9 however when I read the output it is not including decimal places; awk ' BEGIN{FS=OFS=","} NR==1{print;next} {a+=$9 c = $12 d = $18 } END{for(i in a) {split(i,b,";"); print $1, $2, $3, b, $5, $6, b, b, a, $10, $11,... (1 Reply)
Discussion started by: Ads89
1 Replies

3. Shell Programming and Scripting

How to sum the matrix using awk?

input A1 B1 A2 B2 0 0 1 1 1 0 0 1 0 1 1 0 1 1 1 1 Output label A1 B1 A2 B2 A1 2 1 1 2 B1 1 2 2 1 A2 1 2 3 2 B2 2 1 2 3 Ex: The number of times that A1 and B1 row values are both 1 should be printed as output. The last row of A1 and B1 in the input match by having 1 in both... (4 Replies)
Discussion started by: quincyjones
4 Replies

4. Shell Programming and Scripting

Sum value using sed or awk ?

Hello all, how would one go about writing a command using sed/awk that will give me an output that can sum up the number of time each user has done something and also add the amount of time... so output would be for example "smiths has run 3 process and for time taken of value: 224" ... (5 Replies)
Discussion started by: crazy_max
5 Replies

5. Shell Programming and Scripting

awk and count sum ?

I have a input.txt file which have 3 fields separate by a comma place, os and timediff in seconds tampa,win7, 2575 tampa,win7, 157619 tampa,win7, 3352 dallas,vista,604799 greenbay,winxp, 14400 greenbay,win7 , 518400 san jose,winxp, 228121 san jose,winxp, 70853 san jose,winxp, 193514... (5 Replies)
Discussion started by: sabercats
5 Replies

6. Shell Programming and Scripting

Sum using awk

Hi all, I need to sum values for fields in a delimited file as below: 2010-03-05||| 2010-03-05|||123 2010-03-05|467.621|369.532| 2010-03-06||| 2010-03-06||2| 2010-03-06|||444 2010-03-07||| 2010-03-07||| 2010-03-07|655.456|1019.301| Code used is: nawk -F "|" ' { sum +=... (7 Replies)
Discussion started by: Katabatic
7 Replies

7. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

8. Shell Programming and Scripting

Sum with awk

Hi,consider this fields, $1 $2 $3 981 0 1 984 0 4 985 1 0 987 0 2 990 0 0 993 0 3 995 2 0 996 0 1 999 0 4 for each occurence of zero in column $2 and $3 I need to sum $1 fields, so for example, in this piece of code the result of $1 is 8910. I'm sure... (2 Replies)
Discussion started by: cv313x
2 Replies

9. Shell Programming and Scripting

Awk question: Sum dates

Hi there, I have a logfile which has the following layout: 20080812 0 20 20080812 12 10 20080812 12 10 20080812 12 10 I want to sum the "12" on the last 3 lines and save the "20" on the first line. The final output should be 20080812 36 20 I think that should me more easier with... (6 Replies)
Discussion started by: BufferExploder
6 Replies

10. UNIX for Dummies Questions & Answers

Column containing sum using awk

Hi All, I am trying to add a column that contains the sum of the previous column repeated. IE 1 2 3 4 I would like to get: 1 10 2 10 3 10 4 10 Advice? I can get 1 1 2 3 3 6 (4 Replies)
Discussion started by: baconbasher
4 Replies
Login or Register to Ask a Question