Convert Epoch to timestamp in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Convert Epoch to timestamp in a file
# 1  
Old 03-22-2016
Lightbulb Convert Epoch to timestamp in a file

Hi Team,

Could you please let me know ,how to convert Epoch column to timestamp in a flat file.

Code:
"57894"|"1454247163111"|"""HH"""
"57897"|"1454247163111"|"""HH"""
"7906"|"1454247163111"|"""ss"""

I want second field as timestamp.
Moderator's Comments:
Mod Comment This thread has been moved from a non-technical forum to a technical forum and the spelling in the topic has been corrected.

Last edited by Don Cragun; 03-22-2016 at 04:08 AM.. Reason: Note thread movement.
# 2  
Old 03-22-2016
Hello shabeena,

Could you please try following and let me know if this helps.
Code:
echo "1458628242" | awk '{ printf "%s -- %s\n", strftime("%c",$1), $0 }'

Output will be as follows.
Code:
Tue 22 Mar 2016 02:30:42 AM EDT -- 1458628242

EDIT: Seems you have added Input_file now, then following may help you in same now.
Code:
awk -F"|" '{print strftime("%c",$2)}'  Input_file

Output will be as follows.
Code:
Wed 31 Dec 1969 07:00:00 PM EST
Wed 31 Dec 1969 07:00:00 PM EST
Wed 31 Dec 1969 07:00:00 PM EST

In case you have any other requirements other than this, then request you to please show us complete Input_file with sample output too. Hope this helps.

Thanks,
R. Singh

Last edited by RavinderSingh13; 03-22-2016 at 03:45 AM.. Reason: Added solution as per user's Input_file now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 03-22-2016
Hi Ravinder,

it is working fine. I want it to change in whole file for 2nd column.

Note: I have to load data into Teradata table, so I required it to convert into timestamp(6) format.
# 4  
Old 03-22-2016
Hello shabeena,

Could you please try following and let me know if this helps you.
Code:
awk -F"|" '{$2=strftime("%c",$2)}; 1' OFS="|"  Input_file > Temp_Input_file
mv Temp_Input_file Input_file

I have given only commands as above, you could use them in script too.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 03-22-2016
Hi Ravinder,

its working Smilie

iam getting <<Thu 01 Jan 1970 10:00:00 AM AEST>>

I want the data as <<1970-01-01 10:00:00>>
# 6  
Old 03-22-2016
Quote:
Originally Posted by shabeena
Hi Ravinder,
its working Smilie
iam getting <<Thu 01 Jan 1970 10:00:00 AM AEST>>
I want the data as <<1970-01-01 10:00:00>>
Hello Shabeena,

Could you please try following and let me know if this helps you.
Code:
awk -F"|" '{$2=strftime("%c",$2);num=split("Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec", month," ");for(i=1;i<=num;i++){B[month[i]]=i};split($2, A," ");$2=A[4]"-"B[A[3]]"-"A[2] " " A[5]}; 1' OFS="|"  Input_file

Output will be as follows on same.
Code:
"57894"|1969-12-31 07:00:00|"""HH"""
"57897"|1969-12-31 07:00:00|"""HH"""
"7906"|1969-12-31 07:00:00|"""ss"""

Thanks,
R. Singh
# 7  
Old 03-22-2016
Are you sure 1969-12-31 is a correct conversion of an epoch time like 1458628242? The former is one day before the epoch:
Code:
date -d@-86400
Mi 31. Dez 01:00:00 CET 1969

---------- Post updated at 13:03 ---------- Previous update was at 13:00 ----------

And, BTW, the "epoch times" in post#1 are ambitious as well:
Code:
date -d@1454247163111
Fr 4. Apr 11:18:31 CEST 48053

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert a string to epoch time

Team, I am working on a shell script and i am extracting a date string in "SunOS server" with below format. Mon Jan 21 04:13:48 EST 2021 Can you please assist me the best way to convert the extracted string to epoch time like "date +%s" in Linux. Thanks in advance (1 Reply)
Discussion started by: Girish19
1 Replies

2. Shell Programming and Scripting

AIX : Need to convert UNIX Timestamp to normal timestamp

Hello , I am working on AIX. I have to convert Unix timestamp to normal timestamp. Below is the file. The Unix timestamp will always be preceded by EFFECTIVE_TIME as first field as shown and there could be multiple EFFECTIVE_TIME in the file : 3.txt Contents of... (6 Replies)
Discussion started by: rahul2662
6 Replies

3. Shell Programming and Scripting

Convert UNIX timestamp to readable format in the file

Hello I have a file : file1.txt with the below contents : 237176 test1 test2 1442149024 237138 test3 test4 1442121300 237171 test5 test7 1442112823 237145 test9 test10 1442109600 In the above file fourth field represents the timestamp in Unix format. I found a command which converts... (6 Replies)
Discussion started by: rahul2662
6 Replies

4. Shell Programming and Scripting

Convert to epoch time

how can i modify the following command to instead provide the epoch time of the interfaces file? perl -le 'print scalar localtime ((stat "/home/skysmart/interfaces"))' Tue Feb 19 03:44:52 2013 i'm hoping to get the equivalent of this command: stat --format=%Y /home/skysmart/interfaces ... (2 Replies)
Discussion started by: SkySmart
2 Replies

5. Shell Programming and Scripting

Epoch & Unix Timestamp Conversion Tools

Hi All, Please read the below data carefully. I need an unix command for converting unix timestamp to Epoch timestamp. I need to daily convert this today's unix(UTC) time to epoch time, so i am thinking to make a shellscript for this. Please help me for this by providing... (3 Replies)
Discussion started by: aish11
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

Convert value stored in a variable to epoch time?

Hello I have a the creation date of a file stored in a variable in the following format: Wed May 06 10:14:58 2009Is there a way I can echo the variable and display it in epoch time? I've done a lot of searching on this topic, but haven't managed to get a solution. I'm on Solaris 10. ... (2 Replies)
Discussion started by: Glyn_Mo
2 Replies

8. Shell Programming and Scripting

Convert date into epoch in Perl on Solaris

Solaris 10 doesn't seem to like me a lot. I am trying to run a simple script to accept date and return epoch of that date: #!/usr/bin/perl -w use strict; use Time::ParseDate; my($date1)="Mon Mar 27 05:54:08 CDT 2009"; chomp $date1; #Convert to seconds since start of epoch my $time1 =... (3 Replies)
Discussion started by: pavanlimo
3 Replies

9. Shell Programming and Scripting

conversion from EPOCH timestamp to local time zone

hello gurus, i want a perl/shell script which once invoked should convert a set of EPOCH timestamps to local time ( IST..i want) . how does it work ,i have an idea on that..but writing a perl/shell script for it is not possible for me...so i need help for the same. my exact requirement is... (2 Replies)
Discussion started by: abhijeetkul
2 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