# Command to convert a column from epoch time to standard time



## skeelol (May 11, 2013)

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:


```
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:


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

But that's not working either. Can anybody give me some pointers?


----------



## wblock@ (May 11, 2013)

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.


----------



## skeelol (May 11, 2013)

Here is a sample:


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


----------



## cpm@ (May 11, 2013)

Using gawk(1), e.g. 

```
[CMD]% gawk 'BEGIN{print strftime("%Y-%m-%d",1319583934)}'[/CMD]
2011-10-26
```

Or using perl(1) as @wblock@ was suggested:

```
[CMD]% date +%s[/CMD]
1368295573
[CMD]% perl -e "print scalar(localtime(1368295573))"[/CMD]
Sat May 11 20:06:13 2013
```


----------



## wblock@ (May 11, 2013)

Yes, but you have to print the other fields also:

```
% 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
```


----------



## skeelol (May 11, 2013)

Great! I've customized to display the way I want:


```
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?


----------



## wblock@ (May 11, 2013)

The easiest way is to make the command an executable shell script and call that from the alias.


----------



## cpm@ (May 11, 2013)

First, create access as following:

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

```
#!/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:

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


----------



## skeelol (May 11, 2013)

Super! Thanks for the help.


----------



## kpa (May 11, 2013)

`{ while read epoch foo bar ip ; do date -r $epoch +%Y-%m-%d; done } <access`


----------

