Using awk or nawk to convert epoch time to date format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk or nawk to convert epoch time to date format
# 1  
Old 10-27-2011
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 system (Solaris) and use A command to convert it to a standard date format of my choosing.



Why not just use date '+%format'?


One main reason is that I need the date 12 hours in the future as a variable for a command that will be executed at a given point in time. If there is a way to do this using just date, I'm all ears. I know with GNU date you can pull epoch with %s, format the time and even add time to the value returned by date using options like "+12 hours" or "-1 day", etc.
These options do not work with date on Solaris.


Why not just use Perl or another program language?

Right now there are no options to deploy additional functionality, not even updates to the packages like gawk (which works, btw....GRRRRR!!!!), so we must work within the confines of awk or nawk or whatever else will work. Perl is not installed on the Solaris machines and unfortunately is not an option. Smilie


What is needed.

So far I can get the epoch value, but for my variable I need to have the date 12 hours in the future in the following format:


MMDDYYYY
THHMMSS (the 'T' is a delimiter for the program reading the value)

I can achieve this with the current date and time, but not with future date and time. Because I am using Solaris, I cannot use gawk (on this build) and am limited to either awk or nawk as available commands. Again, this is with my limited knowledge. If there is another command that can take epoch time and convert to a date format, please educate me.
[FONT=Arial][SIZE=2]


What works, what doesn't.

So far I have tried the following but with syntax errors that are vague at best. This seems like it would be the best solution if I could figure out what Solaris doesn't like about the syntax. I can copy this text and input it directly into a Linux command line and I get what I want, so it's not a matter of typing. Smilie

Code:
$ awk 'BEGIN{print strftime("%m%d%YT%H%M%S", '1154020897')}'
awk: syntax error near line 1
awk: illegal statement near line 1
$

This works in Linux (Fedora 14), but not in Solaris 10.

I'm using nawk this way to get the epoch time.

Code:
nawk 'BEGIN{print srand()}')

For the date, I am using this for my start date, but so far I haven't seen a method to calculate the time 12 hours in the future.

Code:
date '+%m%d%YT%H%M%S'

So is there any hope? Does anyone have any suggestions that may prove effective?

Thanks in advance for the help!

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by minigts; 10-27-2011 at 01:53 PM.. Reason: code tags rm color, updated Perl comment
# 2  
Old 10-27-2011
Code:
perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=gmtime(time);$year+=1900;$mon="0$mon" if length$mon==1;$mday="0$mday" if length$mday==1;$hour-=12 if $hour>12;print"$mon-$mday-$year | $hour:$min:$sec\n"'

or if you need script:
Code:
#!/usr/bin/perl -w

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=gmtime(time);
$year+=1900;
$mon="0$mon" if length$mon==1;
$mday="0$mday" if length$mday==1;
$hour-=12 if $hour>12;
print"$mon-$mday-$year | $hour:$min:$sec\n";

---------- Post updated at 08:38 PM ---------- Previous update was at 08:34 PM ----------

actualy you will need to do the same for $sec $min $hour as i did for $mon and $mday
they may have single digit too

you can change 'gmtime' for 'localtime' if you prefer
tip78
# 3  
Old 10-27-2011
Thanks for the quick reply, but Perl is not an option. If it were, this would have been resolved before it was an issue. Smilie
# 4  
Old 10-27-2011
Quote:
Why not just use Perl or another program language?

Right now there are no options to deploy additional functionality, not even updates to the packages like gawk (which works, btw....GRRRRR!!!!), so we must work within the confines of awk or nawk or whatever else will work.


i didn't get the "additional functionality" thing.. perl exists on any platform and this simple script doesn't require any "additional functionality"
tip78
# 5  
Old 10-27-2011
Gotcha. The least common denominator is what we have to go by and some of the Solaris machines do not have Perl installed. Not sure if this company policy or what, but that is the limiting factor.

That's a good point though. I'll update my original post to reflect this limitation.
# 6  
Old 10-27-2011
Code:
#!/bin/ksh

# here's my current EPOCH time
currentEpoch=$(/usr/bin/truss /usr/bin/date 2>&1 | /usr/bin/awk '/^time/ {print $NF}')
future12=$(($currentEpoch + 43200))

# here we convert EPOCH back to human readable format
back2Human=$(echo "0t${currentEpoch}=Y" | /usr/bin/adb)

Or look through the FAQs
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Date format conversion how to change this from using nawk to awk

Hi, I have a file where I need to change the date format on the nth field from DD-MM-YYYY to YYYY-MM-DD so I can accurately sort the record by dates From regex - Use sed or awk to fix date format - Stack Overflow, I found an example using nawk. Test run as below: $: cat xyz.txt A ... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Convert Second Column Date Into EPOCH Time And Print Complete Row

Hello Team, I am stuck in getting the required output in the following case. Please help. My input file is aa|08/01/2016 bb|08/15/2016 I wish to convert the file into aa|epoch time bb|epoch time I am using following code: (3 Replies)
Discussion started by: angshuman
3 Replies

3. Shell Programming and Scripting

Convert epoch time stamp into human readable format

Can someone help me to write a shell script to convert epoch timestamp into human readable format 1394553600,"test","79799776.0","19073982.728571","77547576.0","18835699.285714" 1394553600,"test1","80156064.0","19191275.014286","62475360.000000","14200554.720000"... (10 Replies)
Discussion started by: Moon1234
10 Replies

4. Shell Programming and Scripting

Convert epoch time to Julian date

Need assistance in converting an epoch time to Julian date To get epoch perl -e 'use Time::Local; print timelocal(1,5,2,12,10,2008), "\n"' (3 Replies)
Discussion started by: ajayram_arya
3 Replies

5. Shell Programming and Scripting

Convert epoch to human readable date & time format

Hello I have log file from solaris system which has date field converted by Java application using System.currentTimeMillis() function, example is 1280943608380 which equivalent to GMT: Wed, 04 Aug 2010 17:40:08 GMT. Now I need a function in shell script which will convert 1280943608380... (3 Replies)
Discussion started by: Yaminib
3 Replies

6. Shell Programming and Scripting

Need to convert an epoch date to MMDDYYHHmm format

System: HP-UX Kornshell Perl is installed, but not POSIX Hello, I am calculating a future date/time. To do this I take the system date in epoch format and add to it. I now need to take the new epoch date and convert it to MMDDYYHHmm format. Any help with this is greatly appreciated. (4 Replies)
Discussion started by: LetsGoPens
4 Replies

7. 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

8. 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

9. 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

10. Shell Programming and Scripting

how to convert epoch time to readible format?

Hi, I would like to convert epoch time from the logs to readible fromat. How do I do it within shell? Thanks! (11 Replies)
Discussion started by: cin2000
11 Replies
Login or Register to Ask a Question