time conversion using nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting time conversion using nawk
# 1  
Old 04-10-2011
time conversion using nawk

hi,

i've got an input file that contains
Code:
12345678 AAA
12345679 BBB
12345680 CCC

where 1st column is epoch time while the second column are some string.

I'm using nawk to do regular expression on the 2nd column and i need to convert the epoch time to human readable time e.g. 'yyyymmdd hh24:mi:ss'.

i cant find any info on time converion in nawk .. any suggestions on how i can do it in nawk?
# 2  
Old 04-10-2011
Code:
miro@miro-ntb:~/Desktop$ date +%s | awk '{print strftime("%Y-%m-%d %H:%M:%S",$1)}'
2011-04-10 15:37:14

# 3  
Old 04-10-2011
As far as I know, nawk does not support strftime(). It is only supported by gawk. If gawk is available to you, Mirni's answer will work for you.
# 4  
Old 04-11-2011
How about using 'date'?
Code:
 
$ epoch=1302510354 ; echo $epoch | awk '{system("date -d @" $1 " +\"%Y%m%d %H:%M:%S\""); }'
20110410 22:25:54

# 5  
Old 04-11-2011
hi,

thanx for the prompt response.

$1 contains the epoch time, thus i would need to do conversion on $1 ..
also i've have to read multiple files and convert n output them to their respective files ..

am not sure abt using gawk .. i'll try Smilie
# 6  
Old 04-11-2011
Perl
Code:
perl -nle 'use Time::Local;@dt=localtime((split)[0]);printf "%4d%02d%02d%s\n",$dt[5]+1900,$dt[4]+1,$dt[3]," $dt[2]:$dt[1]:$dt[0]";' inputfile

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

GMT to local Time conversion

How can I convert the following date format: New Log Date = 2016-12-30 23:50:33 GMT from GMT time to local time? Thanks (13 Replies)
Discussion started by: mrn6430
13 Replies

3. Programming

Python conversion to epoch time

Hi. I have timestamps that I am trying to convert to epoch time. An example: I am trying to convert this to an epoch timestamp but have one little glitch. I have this: import time date_time = '' pattern = '' epoch = int(time.mktime(time.strptime(date_time, pattern))) print epoch... (1 Reply)
Discussion started by: treesloth
1 Replies

4. Shell Programming and Scripting

NAWK conversion of hexadecimal input to decimal output via printf, I am close I can feel it

I have searched and the answers I have found thus far have led me to this point, so I feel I am just about there. I am trying to convert a column of hexadecimal to decimal values so that I can filter out via grep just the data I want. I was able to pull my original 3 character hex value and... (10 Replies)
Discussion started by: PCGameGuy
10 Replies

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

6. Shell Programming and Scripting

nawk time substracting

Nawk ‘BEGIN {printf “0t%d=Y\n”,srand () -1800}' | adb I know that printf is printing the value in %d which is for a day of the month and subtracting the value by -1800 (30 minutes). So if date is 14:25:45 subtracting from 1800 then time will equal to 13:55:45 I also know that srand () is... (1 Reply)
Discussion started by: INHF
1 Replies

7. Shell Programming and Scripting

time conversion

My local server is in UTC time whereas my remote server in in CST time. I get a sequential file from CST time server and the records in will contain the first line as a filename and the second line as date time like mentioned below. /abc/cde/f1.txt 2009-04-28 23:10:05 CST Now i need to... (3 Replies)
Discussion started by: HemaV
3 Replies

8. Programming

Conversion of time structure in C++

Hi frnds, I have written a code to get current time struct tm *locTime; time_t currentTime; time( &currentTime ); locTime = localtime(&currentTime ); Suppose now i get output time as 31-DEC-2007 00:00:00 What i want now is i want two strings where in one string i want to subtract... (1 Reply)
Discussion started by: electroon
1 Replies

9. Shell Programming and Scripting

Nawk Time Function

Hi All, I am using solaris and nawk. Is there any time function in nawk which is simliar to the shell `date` function ? Can any experts show any examples? (4 Replies)
Discussion started by: Raynon
4 Replies

10. UNIX for Advanced & Expert Users

Time conversion in Unix

Hi, Is there any way to get current time for a particular country using any unix command ? I mean, If i want to find out current time in US or UK or anything .... Any command or library routine available for that? How it can be done in C on Unix? Can anybody help me? Thanks --... (1 Reply)
Discussion started by: kamlakar
1 Replies
Login or Register to Ask a Question