We have the below records where we need record count of every 30 minute like 00:01 to 00:30 so in that we will have 48 record count in 24 hrs , and also we need sum of record count from 00:01 to 23:30. Please find sample data as well.
00:01 21
00:02 23
00:03 34
00:04 34
00:05 30
00:06 16
00:07 26
00:08 13
00:09 17
00:10 13
00:11 21
00:12 24
00:13 28
00:14 26
00:15 21
00:16 39
00:17 42
00:18 37
00:19 29
00:20 20
00:21 25
00:22 30
00:23 10
00:24 19
00:25 19
00:26 20
00:27 24
00:28 16
00:29 18
00:30 16
00:31 17
00:32 34
00:33 14
00:34 20
00:35 21
00:36 37
00:37 21
00:38 27
00:39 28
00:40 13
00:41 18
00:42 22
00:43 14
00:44 12
00:45 16
00:46 30
00:47 19
00:48 23
00:49 22
00:50 25
00:51 30
00:52 32
00:53 25
00:54 15
00:55 13
00:56 11
00:57 22
00:58 19
00:59 19
01:00 18
01:01 30
01:02 25
01:03 25
01:04 20
01:05 14
01:06 23
01:07 9
01:08 9
01:09 10
01:10 10
01:11 18
01:12 12
01:13 17
01:14 15
01:15 14
01:16 20
01:17 6
01:18 8
01:19 10
01:20 14
01:21 13
01:22 10
01:23 7
01:24 20
01:25 16
01:26 17
01:27 14
01:28 18
01:29 19
01:30 15
01:31 19
01:32 18
01:33 13
01:34 14
01:35 13
01:36 17
01:37 5
01:38 15
01:39 19
01:40 34
01:41 18
01:42 10
01:43 13
01:44 11
01:45 16
01:46 13
01:47 4
01:48 4
01:49 14
01:50 14
01:51 7
01:52 20
01:53 18
01:54 17
01:55 20
01:56 30
01:57 14
01:58 27
01:59 17
02:00 24
02:01 60
02:02 23
02:03 8
02:04 31
02:05 47
02:06 34
02:07 30
02:08 26
02:09 18
02:10 35
02:11 29
02:12 33
02:13 23
02:14 7
For starters, try something like:
awk '{t+=$2} /:[03]0/{print $1,t; t=0}' file
Please let me know also how to sum of record count from 00:01 to 23:30.
Hello nadeemrafikhan,
Could you please try following and let me know if this helps, though not completely sure about your requirement.
awk '{split($1, A,":");if(A[2]==30 || A[2]==59){B=B+$NF;print $1 OFS B;B=""};B=B+$NF} END{if(A[2]=30 && A[2]!=59){print $1 OFS B}}' Input_file
Output will be as follows.
00:30 711
00:59 635
01:30 495
01:59 469
02:14 445
Thanks,
R. Singh
Thanks guys I got the answer of first post but Please let me know also how to sum of record count from 00:01 to 23:30.
Hello nadeemrafikhan,
Could you please try following and let me know if this helps you. Also please do mention all requirements in case this doesn't match your requirement with sample Input_file and sample Output too.
awk '{split($1, A,":");if(A[1]==23 && A[2]==30){print SUM};SUM+=$NF}' Input_file
Thanks,
R. Singh
RudiC
March 15, 2016, 12:48pm
7
Your specification is not too clear, as has been stated before, Try
awk '{SUM += $2; TOT += $2} !(NR%30) {print $1, SUM; SUM = 0} END {print "Total", TOT}' file
00:30 711
01:00 637
01:30 458
02:00 478
Total 2688
Ravinder- Itried this but did'nt got out put
awk '{split($1, A,":");if(A[1]==23 && A[2]==30){print SUM};SUM+=$NF}' a
nothing return
Hello,
I have used same Input_file provided by you, only thing I have added at last 23:59 as per your condition provided. Please note that variable named SUM will be printed only if line has 23:59 value. Let's say following is the Input_file.
cat Input_file
00:01 21
00:02 23
00:03 34
00:04 34
00:05 30
00:06 16
00:07 26
00:08 13
00:09 17
00:10 13
00:11 21
00:12 24
00:13 28
00:14 26
00:15 21
00:16 39
00:17 42
00:18 37
00:19 29
00:20 20
00:21 25
00:22 30
00:23 10
00:24 19
00:25 19
00:26 20
00:27 24
00:28 16
00:29 18
00:30 16
00:31 17
00:32 34
00:33 14
00:34 20
00:35 21
00:36 37
00:37 21
00:38 27
00:39 28
00:40 13
00:41 18
00:42 22
00:43 14
00:44 12
00:45 16
00:46 30
00:47 19
00:48 23
00:49 22
00:50 25
00:51 30
00:52 32
00:53 25
00:54 15
00:55 13
00:56 11
00:57 22
00:58 19
00:59 19
01:00 18
01:01 30
01:02 25
01:03 25
01:04 20
01:05 14
01:06 23
01:07 9
01:08 9
01:09 10
01:10 10
01:11 18
01:12 12
01:13 17
01:14 15
01:15 14
01:16 20
01:17 6
01:18 8
01:19 10
01:20 14
01:21 13
01:22 10
01:23 7
01:24 20
01:25 16
01:26 17
01:27 14
01:28 18
01:29 19
01:30 15
01:31 19
01:32 18
01:33 13
01:34 14
01:35 13
01:36 17
01:37 5
01:38 15
01:39 19
01:40 34
01:41 18
01:42 10
01:43 13
01:44 11
01:45 16
01:46 13
01:47 4
01:48 4
01:49 14
01:50 14
01:51 7
01:52 20
01:53 18
01:54 17
01:55 20
01:56 30
01:57 14
01:58 27
01:59 17
02:00 24
02:01 60
02:02 23
02:03 8
02:04 31
02:05 47
02:06 34
02:07 30
02:08 26
02:09 18
02:10 35
02:11 29
02:12 33
02:13 23
02:14 7
23:30 22
Following is the code then for same.
awk '{split($1, A,":");if(A[1]==23 && A[2]==30){print SUM};SUM+=$NF}' Input_file
Output is as follows.
2688
Let me know the complete details for requirement if above doesn't help you.
Thanks,
R. Singh
sea
March 15, 2016, 2:18pm
10
If you have such a file per day, this will work just fine:
#!/bin/bash
total=0
while read time value;do
total=$(( $total + $value))
done<input_file
echo $total
hth
great Ravinder thanks.
it works.