Awk: time intervals based on epoch time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: time intervals based on epoch time
# 1  
Old 06-15-2015
Awk: time intervals based on epoch time

I have a list of epoch times delimited by "-" as follows:

Code:
1335078000 - 1335176700  
1335340800 - 1335527400
1335771300 - 1335945600
1336201200 - 1336218000

The corresponding dates are:

Code:
20120422 1000 - 20120423 1325 	
20120425 1100 - 20120427 1450 	
20120430 1035 - 20120502 1100 	
20120505 1000 - 20120505 1440

I would like to create 5 minutes time intervals based on epoch time columns, for each row in the input file.

The script I've came with, does not work properly if I have several days on the same row.

Code:
awk -F"-" '
	{
		for (i=$1;i<=$2;i+=300)   #5 minutes time interval
			{
				day = strftime("%j", $1)
				if (strftime("%H%M%S", i) <= strftime("%H%M%S", $2))
					{
						if (i != $1)
							{
								strftime("%H%M%S", i) == strftime("%H%M%S", $1)
								day2 = strftime("%j", $2)
								count++
							}
							
						else	
							{
								if (day == day2)
								         {
								         	count++
								         }
								 else
								 	{
								 		count =1
								 	}
							}
						print strftime("%Y-%m-%d",i),strftime("%H%M%S", i),count
					} 
				else 
					{
						count=1
						$1=i+61200
						i=$1
						print strftime("%Y-%m-%d",i),strftime("%H%M%S", i),count
					}
			}
	}
' input > output.interval

I will use the first row as example to show the script result and desired output

Code:
1335078000 - 1335176700    (20120422 1000 - 20120423 1325)


Code:
2012-04-22 100000 1
2012-04-22 100500 2
2012-04-22 101000 3
2012-04-22 101500 4
2012-04-22 102000 5
2012-04-22 102500 6
2012-04-22 103000 7
2012-04-22 103500 8
2012-04-22 104000 9
2012-04-22 104500 10
2012-04-22 105000 11
2012-04-22 105500 12
2012-04-22 110000 13
2012-04-22 110500 14
2012-04-22 111000 15
2012-04-22 111500 16
2012-04-22 112000 17
2012-04-22 112500 18
2012-04-22 113000 19
2012-04-22 113500 20
2012-04-22 114000 21
2012-04-22 114500 22
2012-04-22 115000 23
2012-04-22 115500 24
2012-04-22 120000 25
2012-04-22 120500 26
2012-04-22 121000 27
2012-04-22 121500 28
2012-04-22 122000 29
2012-04-22 122500 30
2012-04-22 123000 31
2012-04-22 123500 32
2012-04-22 124000 33
2012-04-22 124500 34
2012-04-22 125000 35
2012-04-22 125500 36
2012-04-22 130000 37
2012-04-22 130500 38
2012-04-22 131000 39
2012-04-22 131500 40
2012-04-22 132000 41
2012-04-22 132500 42
2012-04-23 063000 1	 <<<< here should start from 100000 again
2012-04-23 063500 2
2012-04-23 064000 3
2012-04-23 064500 4
2012-04-23 065000 5
2012-04-23 065500 6
2012-04-23 070000 7
2012-04-23 070500 8
2012-04-23 071000 9
2012-04-23 071500 10
2012-04-23 072000 11
2012-04-23 072500 12
2012-04-23 073000 13
2012-04-23 073500 14
2012-04-23 074000 15
2012-04-23 074500 16
2012-04-23 075000 17
2012-04-23 075500 18
2012-04-23 080000 19
2012-04-23 080500 20
2012-04-23 081000 21
2012-04-23 081500 22
2012-04-23 082000 23
2012-04-23 082500 24
2012-04-23 083000 25
2012-04-23 083500 26
2012-04-23 084000 27
2012-04-23 084500 28
2012-04-23 085000 29
2012-04-23 085500 30
2012-04-23 090000 31
2012-04-23 090500 32
2012-04-23 091000 33
2012-04-23 091500 34
2012-04-23 092000 35
2012-04-23 092500 36
2012-04-23 093000 37
2012-04-23 093500 38
2012-04-23 094000 39
2012-04-23 094500 40
2012-04-23 095000 41
2012-04-23 095500 42
2012-04-23 100000 43
2012-04-23 100500 44
2012-04-23 101000 45
2012-04-23 101500 46
2012-04-23 102000 47
2012-04-23 102500 48
2012-04-23 103000 49
2012-04-23 103500 50
2012-04-23 104000 51
2012-04-23 104500 52
2012-04-23 105000 53
2012-04-23 105500 54
2012-04-23 110000 55
2012-04-23 110500 56
2012-04-23 111000 57
2012-04-23 111500 58
2012-04-23 112000 59
2012-04-23 112500 60
2012-04-23 113000 61
2012-04-23 113500 62
2012-04-23 114000 63
2012-04-23 114500 64
2012-04-23 115000 65
2012-04-23 115500 66
2012-04-23 120000 67
2012-04-23 120500 68
2012-04-23 121000 69
2012-04-23 121500 70
2012-04-23 122000 71
2012-04-23 122500 72
2012-04-23 123000 73
2012-04-23 123500 74
2012-04-23 124000 75
2012-04-23 124500 76
2012-04-23 125000 77
2012-04-23 125500 78
2012-04-23 130000 79
2012-04-23 130500 80
2012-04-23 131000 81
2012-04-23 131500 82
2012-04-23 132000 83
2012-04-23 132500 84

Any help will be much appreciated, and if possible I would like to know if are better ways doing this.

Thanks in advance.
# 2  
Old 06-15-2015
You're adding arbitrary 17 hours - why should it advance to 100000 next day (unless you are at 170000)?

Try
Code:
awk -F"-" '

function strftime(FMT,SECS)     {("date \"+" FMT "\" -d@" SECS) | getline TMP
                                 return TMP
                                }

                {gsub (/ /,_)
                 day1 = strftime("%j", $1)
                 day2 = strftime("%j", $2) 
                 timespan = ($2-$1)%86400  
                 for (j=day1; j<=day2; j++)
                        {count=1
                         for (i=0; i<=timespan; i+=300)        
                                print strftime("%Y-%m-%d %H%M%S", i+$1),  count++
                         $1+=86400
                        }
                }

'  input
2012-04-22 090000  1
2012-04-22 090500  2
2012-04-22 091000  3
.
.
.
2012-04-22 122000  41
2012-04-22 122500  42
2012-04-23 090000  1
2012-04-23 090500  2
2012-04-23 091000  3
2012-04-23 091500  4

and report back. My timezone seems an hour off, and, unfortunately, my mawk doesn't have the strftime function so I had to fiddle around with date.

Last edited by RudiC; 06-16-2015 at 03:31 AM..
These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 06-15-2015
Nice solution RudiC! You can set TZ for your awk process like this:

Code:
TZ=GMT-3 awk -F"-" '
...

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 06-15-2015
Thank you both of you for the input. Everything worked just fine.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

2. Shell Programming and Scripting

Converting real time to epoch time

# date +%s -d "Mon Feb 11 02:26:04" 1360567564 # perl -e 'print scalar localtime(1360567564), "\n";' Mon Feb 11 02:26:04 2013 the epoch conversion is working fine. but one of my application needs 13 digit epoch time as input 1359453135154 rather than 10 digit epoch time 1360567564... (3 Replies)
Discussion started by: vivek d r
3 Replies

3. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

4. Shell Programming and Scripting

Using awk or nawk to convert epoch time to date format

Looking for some help and usually when I do a search this site comes up. Hopefully someone can give me a little direction as to how to use one of these two commands to achieve what I'm trying to do. What am I trying to do? I need to take the time value in epoch format returned from the... (5 Replies)
Discussion started by: minigts
5 Replies

5. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

6. Shell Programming and Scripting

how to convert date time to epoch time in solaris

Hi, Is there any easy way to convert date time(stored in shell variable ) to epoch time in solaris box? As +%s is working on linux but not on solaris, also -d option is not working. Any suggestion please? (6 Replies)
Discussion started by: anshuman0507
6 Replies

7. Shell Programming and Scripting

Convert Epoch Time to Standard Date and Time & Vice Versa

Hi guys, I know that this topic has been discuss numerous times, and I have search the net and this forum for it. However, non able to address the problem I faced so far. I am on Solaris Platform and unable to install additional packages like the GNU date and gawk to make use of their... (5 Replies)
Discussion started by: DrivesMeCrazy
5 Replies

8. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies

9. Red Hat

How do sa1/sar time intervals work?

Hi, I have set up sar on my RedHat and Fedora Linux systems. I am running sa1 from cron: 0 8-17 * * 1-5 /usr/lib/sa/sa1 1200 3 & The 1200 and 3 parameters tell sa1 to save data every 1200 seconds (== 20 minutes) and to write 3 times. When I run sar to observe my data, I'll see... (1 Reply)
Discussion started by: mschwage
1 Replies

10. Shell Programming and Scripting

Pls Help me out ... I want to check process status at regular intervals of time

I want to check process status at regular interval of time ... so i ha wirtten this BUT its not working when i placed this peace of code in .sh .. please help me out #!/bin/sh w = ps -ef|grep processname | wc - l echo $w if ; then Banner "Proceesname Problem" else Banner " Running... (5 Replies)
Discussion started by: srinivasvandana
5 Replies
Login or Register to Ask a Question