Command to convert a column from epoch time to standard time

This isn't really programming or scripting but I didn't see a more relevent place in the forums to ask.

I need to convert the first column of a file from unix epoch time to standard time.

First I tried this:

Code:
date -r `cat access | awk '{print $1}'`

However I'm told that won't work because date(1) can't take an entire column of text. I was told to try something like this:

Code:
eval `awk '{ print date -r "$1" }' access`

But that's not working either. Can anybody give me some pointers?
 
It is programming. For each line of a file, you want to replace a date in one format with the same date in a different format. "Replace" is the important word, usually meaning sed(1) or awk(1) are needed. Or Perl, which can do everything either of those can do, only better.

Please post a few sample lines of the input file.
 
Here is a sample:

Code:
1319583934 Staff Account 192.168.1.1 
1323781223 Staff Account 192.168.1.1
1324175167 Staff Account 192.168.1.1 
1324175215 Account Account 192.168.1.2 
1326357405 Staff Account 192.168.1.1 
1326357744 Account Account 192.168.1.2 
1327187220 Account Account 192.168.1.2

Thanks.
 
Using gawk(1), e.g.
Code:
[CMD]% gawk 'BEGIN{print strftime("%Y-%m-%d",1319583934)}'[/CMD]
2011-10-26

Or using perl(1) as @wblock@ was suggested:
Code:
[CMD]% date +%s[/CMD]
1368295573
[CMD]% perl -e "print scalar(localtime(1368295573))"[/CMD]
Sat May 11 20:06:13 2013
 
Last edited by a moderator:
Yes, but you have to print the other fields also:
Code:
% gawk '{ print strftime("%Y-%m-%d ",$1),$2,$3,$4 }' testfile
2011-10-25  Staff Account 192.168.1.1
2011-12-13  Staff Account 192.168.1.1
2011-12-17  Staff Account 192.168.1.1
2011-12-17  Account Account 192.168.1.2
2012-01-12  Staff Account 192.168.1.1
2012-01-12  Account Account 192.168.1.2
2012-01-21  Account Account 192.168.1.2
 
Great! I've customized to display the way I want:

Code:
gawk '{ print strftime("%m-%d-%Y %H:%M ",$1),$2,$3,$4 }' access

Works great from command line, how can I get it to function properly as an alias that I've put in my .cshrc?
 
First, create access as following:
Code:
1319583934 Staff Account 192.168.1.1 
1323781223 Staff Account 192.168.1.1
1324175167 Staff Account 192.168.1.1 
1324175215 Account Account 192.168.1.2 
1326357405 Staff Account 192.168.1.1 
1326357744 Account Account 192.168.1.2 
1327187220 Account Account 192.168.1.2

Then, use this simple shell script named epochtostandard.sh or whatever name you want give it.
Code:
#!/bin/sh

gawk '{ print strftime("%m-%d-%Y %H:%M",$1),$2,$3,$4 }' access

Finally, run % sh epochtostandard.sh for convert from epoch time format to local time:
Code:
10-26-2011 01:05 Staff Account 192.168.1.1
12-13-2011 14:00 Staff Account 192.168.1.1
12-18-2011 03:26 Staff Account 192.168.1.1
12-18-2011 03:26 Account Account 192.168.1.2
01-12-2012 09:36 Staff Account 192.168.1.1
01-12-2012 09:42 Account Account 192.168.1.2
01-22-2012 00:07 Account Account 192.168.1.2
 
Back
Top