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 this into actual timestamp :

cat file1.txt | awk '{print $4}' | perl -pe 's/([\d]{10})/localtime $1/eg;'
Sun Sep 13 08:57:04 2015
Sun Sep 13 01:15:00 2015
Sat Sep 12 22:53:43 2015
Sat Sep 12 22:00:00 2015

How can i convert the Unix timestamp to the actual timestamp and replace it with the fourth field in the file. The Output should look like below :

237176 test1 test2 Sun Sep 13 08:57:04 2015
237138 test3 test4 Sun Sep 13 01:15:00 2015
237171 test5 test7 Sat Sep 12 22:53:43 2015
237145 test9 test10 Sat Sep 12 22:00:00 2015

Please help. Thanks

Hello rahul2662,

Following may help you in same.

awk '{ printf "%d %s %s  %s\n", $1, $2, $3, strftime("%c",$NF) }' Input_file

Output will be as follows.

237176 test1 test2  Sun Sep 13 08:57:04 2015
237138 test3 test4  Sun Sep 13 01:15:00 2015
237171 test5 test7  Sat Sep 12 22:53:43 2015
237145 test9 test10  Sat Sep 12 22:00:00 2015

Thanks,
R. Singh

2 Likes

How about

while read A B C TM; do LC_ALL=C printf "%s %s %s %(%c)T\n" $A $B $C $TM; done <file
237176 test1 test2 Sun Sep 13 14:57:04 2015
237138 test3 test4 Sun Sep 13 07:15:00 2015
237171 test5 test7 Sun Sep 13 04:53:43 2015
237145 test9 test10 Sun Sep 13 04:00:00 2015

(needs a recent shell)

1 Like

Thanks Ravinder and RudiC. It worked fine.

---------- Post updated at 07:11 PM ---------- Previous update was at 06:54 PM ----------

One more question :

Suppose I have a file file1.txt with the below contents :

a b c e
f g h i j
m y t
EFFECTIVE_TIME 1373938000
l r t o

The second field on Line number 4 is time ( this could be on any row but will always have EFFECTIVE_TIME as first column) which I want to convert into human readable time. So how can we convert this into real time.

Thanks for your help.

Hello Rahul,

You could try as follows for same, let me know if this helps you.

awk '/EFFECTIVE_TIME/ { printf "%s %s\n", $1, strftime("%c",$NF); next} 1'  Input_file
 

Output will be as follows.

a b c e
f g h i j
m y t
EFFECTIVE_TIME Mon Jul 15 21:26:40 2013
l r t o
 

Thanks,
R. Singh

1 Like

Hello Ravinder what does highlighted "1" means in the below script ?

awk '/EFFECTIVE_TIME/ { printf "%s %s\n", $1, strftime("%c",$NF); next} 1'  Input_file

Hello Rahul,

Following is the explanation for same.

awk '/EFFECTIVE_TIME/                                   #### Searching for string EFFECTIVE_TIME
 { printf "%s %s\n", $1, strftime("%c",$NF);             #### if above condition is TRUE and a line has string EFFECTIVE_TIME then print the first column and chage last column to actual human readbable time.
 next}                                                   #### Now leave all coming statments/actions by calling next here
 1                                                      #### awk works on method of conditions and then perform actions, by 1 we are making condition as TRUE and not mentioning the action here so awk is performing the default action which is print so it will print al lthe lines
'  Input_file                                           #### mentioning Input_file here.
 

Thanks,
R. Singh

1 Like