shijie xu
shijie xu

Reputation: 2097

How to convert timestamp files to date format in unix shell

I have a file, each line of which is timestamp. I want to convert each line into date format. The file content is like:

1370189811
1370189830
1370189851
1370189859
1370189889
1370189890
1370188951
1370188955
1370188976
1370188981

I first convent file into this format:

1970-01-01 UTC 1370189811 seconds
1970-01-01 UTC 1370189830 seconds
1970-01-01 UTC 1370189851 seconds
1970-01-01 UTC 1370189859 seconds
1970-01-01 UTC 1370189889 seconds
1970-01-01 UTC 1370189890 seconds
1970-01-01 UTC 1370188951 seconds
1970-01-01 UTC 1370188955 seconds
1970-01-01 UTC 1370188976 seconds
1970-01-01 UTC 1370188981 seconds

with command awk: awk '{ printf ("1970-01-01 UTC %10d seconds\n" , $1) }' test > test1

Then for each line: 1970-01-01 UTC 1370188981 seconds, I want to exec date -u on each line. For example:

shijiex@localhost:~/Desktop$ date -d '1970-01-01 UTC 1284117137 seconds' 
Fri Sep 10 19:12:17 CST 2010

That's will output what the date corresponding to this timestamp..

.. I wrote:

awk  '{date -d $1}' test1 

but none output.. How should I write this awk command? Thanks

Upvotes: 1

Views: 3818

Answers (1)

cnicutar
cnicutar

Reputation: 182619

Here's a start:

$ awk '{ print(strftime("%F", $1), $1) }' file

You can tweak your strftime format until you get what you want.

Upvotes: 2

Related Questions