How to get the sum?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the sum?
# 1  
Old 09-01-2015
How to get the sum?

hi Gurus,

I have an extract as seen below:

INPUT
Code:
2015-08-24 15:00:00.0 |TCSERVER01 |ServiceEventHandler |2283
2015-08-24 15:01:00.0 |TCSERVER01 |ServiceEventHandler |576
2015-08-24 15:02:00.0 |TCSERVER01 |ServiceEventHandler |833
2015-08-24 15:03:00.0 |TCSERVER01 |ServiceEventHandler |927
2015-08-24 15:04:00.0 |TCSERVER01 |ServiceEventHandler |4655
2015-08-24 15:05:00.0 |TCSERVER01 |ServiceEventHandler |3344
2015-08-24 15:06:00.0 |TCSERVER01 |ServiceEventHandler |3045
2015-08-24 15:07:00.0 |TCSERVER01 |ServiceEventHandler |1473
2015-08-24 15:15:00.0 |TCSERVER01 |ServiceEventHandler |14047
2015-08-24 15:16:00.0 |TCSERVER01 |ServiceEventHandler |2473
2015-08-24 15:17:00.0 |TCSERVER01 |ServiceEventHandler |1077
2015-08-24 15:18:00.0 |TCSERVER01 |ServiceEventHandler |2492
2015-08-24 15:19:00.0 |TCSERVER01 |ServiceEventHandler |1268
2015-08-24 15:20:00.0 |TCSERVER01 |ServiceEventHandler |2964
2015-08-24 15:21:00.0 |TCSERVER01 |ServiceEventHandler |604
2015-08-24 15:22:00.0 |TCSERVER01 |ServiceEventHandler |422
2015-08-24 15:23:00.0 |TCSERVER01 |ServiceEventHandler |1677
2015-08-24 15:24:00.0 |TCSERVER01 |ServiceEventHandler |3263
2015-08-24 15:25:00.0 |TCSERVER01 |ServiceEventHandler |1180
2015-08-24 15:26:00.0 |TCSERVER01 |ServiceEventHandler |1997
2015-08-24 15:27:00.0 |TCSERVER01 |ServiceEventHandler |520
2015-08-24 15:28:00.0 |TCSERVER01 |ServiceEventHandler |599
2015-08-24 15:29:00.0 |TCSERVER01 |ServiceEventHandler |1766
2015-08-24 15:30:00.0 |TCSERVER01 |ServiceEventHandler |1783
2015-08-24 15:31:00.0 |TCSERVER01 |ServiceEventHandler |872
2015-08-24 15:32:00.0 |TCSERVER01 |ServiceEventHandler |921
2015-08-24 15:33:00.0 |TCSERVER01 |ServiceEventHandler |623
2015-08-24 15:34:00.0 |TCSERVER01 |ServiceEventHandler |1080
2015-08-24 15:35:00.0 |TCSERVER01 |ServiceEventHandler |410
2015-08-24 15:36:00.0 |TCSERVER01 |ServiceEventHandler |686
2015-08-24 15:37:00.0 |TCSERVER01 |ServiceEventHandler |1598
2015-08-24 15:38:00.0 |TCSERVER01 |ServiceEventHandler |1874
2015-08-24 15:39:00.0 |TCSERVER01 |ServiceEventHandler |1232
2015-08-24 15:40:00.0 |TCSERVER01 |ServiceEventHandler |2138
2015-08-24 15:41:00.0 |TCSERVER01 |ServiceEventHandler |1199
2015-08-24 15:42:00.0 |TCSERVER01 |ServiceEventHandler |1041
2015-08-24 15:43:00.0 |TCSERVER01 |ServiceEventHandler |1551
2015-08-24 15:44:00.0 |TCSERVER01 |ServiceEventHandler |1197
2015-08-24 15:45:00.0 |TCSERVER01 |ServiceEventHandler |1046
2015-08-24 15:46:00.0 |TCSERVER01 |ServiceEventHandler |375
2015-08-24 15:47:00.0 |TCSERVER01 |ServiceEventHandler |470
2015-08-24 15:48:00.0 |TCSERVER01 |ServiceEventHandler |839
2015-08-24 15:49:00.0 |TCSERVER01 |ServiceEventHandler |1384
2015-08-24 15:50:00.0 |TCSERVER01 |ServiceEventHandler |1427
2015-08-24 15:51:00.0 |TCSERVER01 |ServiceEventHandler |458
2015-08-24 15:52:00.0 |TCSERVER01 |ServiceEventHandler |164
2015-08-24 15:53:00.0 |TCSERVER01 |ServiceEventHandler |1600
2015-08-24 15:54:00.0 |TCSERVER01 |ServiceEventHandler |2099
2015-08-24 15:55:00.0 |TCSERVER01 |ServiceEventHandler |2727
2015-08-24 15:56:00.0 |TCSERVER01 |ServiceEventHandler |1097
2015-08-24 15:57:00.0 |TCSERVER01 |ServiceEventHandler |726
2015-08-24 15:58:00.0 |TCSERVER01 |ServiceEventHandler |385
2015-08-24 15:59:00.0 |TCSERVER01 |ServiceEventHandler |1322
2015-08-24 16:00:00.0 |TCSERVER01 |ServiceEventHandler |2283
2015-08-24 16:01:00.0 |TCSERVER01 |ServiceEventHandler |576
2015-08-24 16:02:00.0 |TCSERVER01 |ServiceEventHandler |833
2015-08-24 16:03:00.0 |TCSERVER01 |ServiceEventHandler |927
2015-08-24 16:04:00.0 |TCSERVER01 |ServiceEventHandler |4655
2015-08-24 16:05:00.0 |TCSERVER01 |ServiceEventHandler |3344
2015-08-24 16:06:00.0 |TCSERVER01 |ServiceEventHandler |3045
2015-08-24 16:07:00.0 |TCSERVER01 |ServiceEventHandler |1473
2015-08-24 16:16:00.0 |TCSERVER01 |ServiceEventHandler |16520
2015-08-24 16:17:00.0 |TCSERVER01 |ServiceEventHandler |1077
2015-08-24 16:18:00.0 |TCSERVER01 |ServiceEventHandler |2492
2015-08-24 16:19:00.0 |TCSERVER01 |ServiceEventHandler |1268
2015-08-24 16:20:00.0 |TCSERVER01 |ServiceEventHandler |2964
2015-08-24 16:21:00.0 |TCSERVER01 |ServiceEventHandler |604
2015-08-24 16:22:00.0 |TCSERVER01 |ServiceEventHandler |422
2015-08-24 16:23:00.0 |TCSERVER01 |ServiceEventHandler |1677
2015-08-24 16:24:00.0 |TCSERVER01 |ServiceEventHandler |3263
2015-08-24 16:25:00.0 |TCSERVER01 |ServiceEventHandler |1180
2015-08-24 16:26:00.0 |TCSERVER01 |ServiceEventHandler |1997
2015-08-24 16:27:00.0 |TCSERVER01 |ServiceEventHandler |520
2015-08-24 16:28:00.0 |TCSERVER01 |ServiceEventHandler |599
2015-08-24 16:29:00.0 |TCSERVER01 |ServiceEventHandler |1766
2015-08-24 16:30:00.0 |TCSERVER01 |ServiceEventHandler |1783
2015-08-24 16:31:00.0 |TCSERVER01 |ServiceEventHandler |872
2015-08-24 16:32:00.0 |TCSERVER01 |ServiceEventHandler |921
2015-08-24 16:33:00.0 |TCSERVER01 |ServiceEventHandler |623
2015-08-24 16:34:00.0 |TCSERVER01 |ServiceEventHandler |1080
2015-08-24 16:35:00.0 |TCSERVER01 |ServiceEventHandler |410
2015-08-24 16:36:00.0 |TCSERVER01 |ServiceEventHandler |686
2015-08-24 16:37:00.0 |TCSERVER01 |ServiceEventHandler |1598
2015-08-24 16:38:00.0 |TCSERVER01 |ServiceEventHandler |1874
2015-08-24 16:39:00.0 |TCSERVER01 |ServiceEventHandler |1232
2015-08-24 16:40:00.0 |TCSERVER01 |ServiceEventHandler |2138
2015-08-24 16:41:00.0 |TCSERVER01 |ServiceEventHandler |1199
2015-08-24 16:42:00.0 |TCSERVER01 |ServiceEventHandler |1041
2015-08-24 16:43:00.0 |TCSERVER01 |ServiceEventHandler |1551
2015-08-24 16:44:00.0 |TCSERVER01 |ServiceEventHandler |1197
2015-08-24 16:45:00.0 |TCSERVER01 |ServiceEventHandler |1046
2015-08-24 16:46:00.0 |TCSERVER01 |ServiceEventHandler |375
2015-08-24 16:47:00.0 |TCSERVER01 |ServiceEventHandler |470
2015-08-24 16:48:00.0 |TCSERVER01 |ServiceEventHandler |839
2015-08-24 16:49:00.0 |TCSERVER01 |ServiceEventHandler |1384
2015-08-24 16:50:00.0 |TCSERVER01 |ServiceEventHandler |1427
2015-08-24 16:51:00.0 |TCSERVER01 |ServiceEventHandler |458
2015-08-24 16:52:00.0 |TCSERVER01 |ServiceEventHandler |164
2015-08-24 16:53:00.0 |TCSERVER01 |ServiceEventHandler |1600
2015-08-24 16:54:00.0 |TCSERVER01 |ServiceEventHandler |2099
2015-08-24 16:55:00.0 |TCSERVER01 |ServiceEventHandler |2727
2015-08-24 16:56:00.0 |TCSERVER01 |ServiceEventHandler |1097
2015-08-24 16:57:00.0 |TCSERVER01 |ServiceEventHandler |726
2015-08-24 16:58:00.0 |TCSERVER01 |ServiceEventHandler |385

what i wanted to have is to get the sum, please see desired output below.

OUPUT
Code:
            TCSERVER01
HOUR    ServiceEventHandler
15:00    87809
16:00    86487

Please help.

Thanks.
# 2  
Old 09-01-2015
something like the following should do it...

Code:
perl -ne '$events{$1}+=$2 if/2015-08-24 (\d\d):\d\d:00.0 \|TCSERVER01 \|ServiceEventHandler \|(\d+)/; END {for my $hour (sort keys %events){print "$hour $events{$hour}\n"}'; log_file.log

# 3  
Old 09-01-2015
hi Skrynesaver,

Thanks for the reply, but need to do this in bash.
# 4  
Old 09-01-2015
Hello, reignangel2003,

Following may help you in same.
Code:
 awk 'BEGIN{OFS="\t"; print OFS OFS "TCSERVER01" ORS "HOURS" OFS "ServiceEventHandler"}{sub(/\|/,X,$NF);A[substr($2,1,2)]+=$NF} END{for(i in A){print i":00" OFS A[i]}}' OFS="\t" Input_file

Output will be as follows.
Code:
                 TCSERVER01
HOURS   ServiceEventHandler
15:00   87809
16:00   86487

EDIT: Adding a non one-liner form of solution on same.
Code:
awk 'BEGIN{
                OFS="\t";
                print OFS OFS "TCSERVER01" ORS "HOURS" OFS "ServiceEventHandler"
          }
          {
                sub(/\|/,X,$NF);
                A[substr($2,1,2)]+=$NF
          }
     END  {
                for(i in A){
                                print i":00" OFS A[i]
                           }
          }
    ' OFS="\t" Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-01-2015 at 08:24 AM.. Reason: Added a non one-liner form of solution
# 5  
Old 09-01-2015
This would work in a recent bash:
Code:
while IFS=\| read DAYTM SVR HDL CNT; do HR=${DAYTM:11:2}; ((SUM[$HR]+=$CNT)); SERVER=$SVR; HANDLER=$HDL; done <file1 
echo "          " $SERVER
echo HOUR "     " $HANDLER
for i in ${!SUM[@]}; do echo $i":00     " ${SUM[$i]}; done
         TCSERVER01
HOUR      ServiceEventHandler
15:00     87809
16:00     86487

# 6  
Old 09-02-2015
Hi All,

Thank you for all the help. But i have another question.

i have this similar issue:

INPUT FILE 1
Code:
ServiceEventHandler,
Processed,Percentage
5285337,100%

INPUT FILE 2
Code:
Wallet,
Processed,Percentage
5285337,100%

INPUT FILE 3
Code:
TIME
< 1m
> 1m - 2m
> 2m - 3m
> 3m - 5m
> 5m - 10m
> 10m - 15m
> 15m - 20m
> 20m - 30m
> 30m

i used this code to join all the files
Code:
paste -d',' file3 file1 file2 > file4.csv

please see output i get
Code:
,ServiceEventHandler,,WalletHandler,
,Processed,Percentage,Processed,Percentage
TIME,2839390,100%,39211657,100%
< 1m,,
> 1m - 2m,,
> 2m - 3m,,
> 3m - 5m,,
> 5m - 10m,,
> 10m - 15m,,
> 15m - 20m,,
> 20m - 30m,,
> 30m,,

desired output should is shown below:
Code:
,ServiceEventHandler,,WalletHandler,
TIME,Processed,Percentage,Processed,Percentage
< 1m,2839390,100%,39211657,100%
> 1m - 2m,0,0,0,0
> 2m - 3m,0,0,0,0
> 3m - 5m,0,0,0,0
> 5m - 10m,0,0,0,0
> 10m - 15m,0,0,0,0
> 15m - 20m,0,0,0,0
> 20m - 30m,0,0,0,0
> 30m,0,0,0,0
TOTAL,2839390,100%,39211657,100%

The total is just the sum on each field:
Processed and Percentage

Please help. need it badly

Thanks.
# 7  
Old 09-02-2015
Moderator's Comments:
Mod Comment Please do not use a single thread to discuss multiple issues. Start a new thread for a new topic (as you already did here).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum of Columns

HI Guys, I gave Input file F.Txt ID H1 H2 H3 H4 H5 A 5 6 7 8 9 B 4 65 4 4 7 C 4 4 4 4 4 D 4 4 4 4 4 Output :- ID H1 H2 H3 H4 H5 Total 17 79 19 20 24 Sum of Each Columns (8 Replies)
Discussion started by: pareshkp
8 Replies

2. UNIX for Dummies Questions & Answers

Sum with condition

Dear masters, I have file input 011171646073|12129|12129|A027|20141001|20141015|2014|10|01|2013|10|15 011171646076|12129|12129|A027|20141001|20141012|2014|10|01|2014|09|12 011171646078|12129|12129|A027|20141001|20141015|2014|10|01|2014|10|15... (6 Replies)
Discussion started by: radius
6 Replies

3. Shell Programming and Scripting

Sum of all columns

Hi Friends, I have a file with fields separated with comma. How to print sum of each field of the file? Eg: input file 1,3,6,7 2,1,2,1 0,1,1,0 I want to sum each field separately. Output file 3,5,9,8 Thanks, Suresh (2 Replies)
Discussion started by: suresh3566
2 Replies

4. Shell Programming and Scripting

Format the value of sum

I have a list of values ( in Kb) I have the following code to sum up the values and convert the total to GB cat list 701368101370 101370101370 801554101370 701636101370 101757101370 101876101370 901951101370 And this is the output of my script awk '{ s += $1 } END {... (3 Replies)
Discussion started by: Sara_84
3 Replies

5. Shell Programming and Scripting

How to sum these value.

Hi, Unix Gurus, I need sum values from a file. file format like: 0004004 0000817 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0045000 0004406 the result should be 459227 (817+45000 ... + 4406) anybody can help me out (7 Replies)
Discussion started by: ken002
7 Replies

6. UNIX for Dummies Questions & Answers

Getting the sum

I am trying to get the sum of the first column of a file. When I use the same method for other files it works just fine... for some reason for the file below it gives me an error that I don't understand... I tried looking at different lines of the file and tried different things, but I still... (7 Replies)
Discussion started by: cosmologist
7 Replies

7. Solaris

How to Sum

Hi I need to incorporate a 'sum' as follows into a script and not sure how. I have a variable per line and I need them to be summed, e.g below 1 23 1,456 1 1 34 46 How do I calculate the sum of all these numbers to ouptut the answer ( 1,562) Thanks in advance (3 Replies)
Discussion started by: rob171171
3 Replies

8. Shell Programming and Scripting

Print sum and relative value of the sum

Hi i data looks like this: student 1 Subject1 45 55 Subject2 44 55 Subject3 33 44 // student 2 Subject1 45 55 Subject2 44 55 Subject3 33 44 i would like to sum $2, $3 (marks) and divide each entry in $2 and $3 with their respective sums and print for each student as $4 and... (2 Replies)
Discussion started by: saint2006
2 Replies

9. Shell Programming and Scripting

sum

Hello everyone I need to write a script that sums numbers passed to it as arguments on the command line and displays the results. I must use a for loop and then rewrite it using a while loop. It would have to output something like 10+20+30=60 this is what I have so far fafountain@hfc:~$ vi sum... (1 Reply)
Discussion started by: Blinky85
1 Replies

10. UNIX for Advanced & Expert Users

sum of more than 1 column

Hi I want to sum of 3 columns in file. Example: I want to sum of 3 ,6,8 th columns in file(SUM(3,6,8)). Using awk can sum of single column awk '{a+=$3} END {printf ("%f\n",a)' file_name Thanks inadvance MR (2 Replies)
Discussion started by: mohan705
2 Replies
Login or Register to Ask a Question