Convert UNIX timestamp to readable format in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert UNIX timestamp to readable format in the file
# 1  
Old 09-15-2015
Convert UNIX timestamp to readable format in the file

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

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

Code:
cat file1.txt | awk '{print $4}' | perl -pe 's/([\d]{10})/localtime $1/eg;'

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

Code:
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
# 2  
Old 09-15-2015
Hello rahul2662,

Following may help you in same.
Code:
awk '{ printf "%d %s %s  %s\n", $1, $2, $3, strftime("%c",$NF) }' Input_file

Output will be as follows.
Code:
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
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 09-15-2015
How about
Code:
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)
This User Gave Thanks to RudiC For This Post:
# 4  
Old 09-17-2015
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 :

Code:
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.
# 5  
Old 09-17-2015
Hello Rahul,

You could try as follows for same, let me know if this helps you.
Code:
awk '/EFFECTIVE_TIME/ { printf "%s %s\n", $1, strftime("%c",$NF); next} 1'  Input_file

Output will be as follows.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 09-17-2015
Hello Ravinder what does highlighted "1" means in the below script ?

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

# 7  
Old 09-17-2015
Hello Rahul,

Following is the explanation for same.
Code:
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

Last edited by RavinderSingh13; 09-17-2015 at 12:50 PM..
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Numeric Time to Readable Timestamp - Perl

I am trying to hit an URL using below command and get the data into an excel sheet. wget --user=<<USERID>> --pass=<<PASSWROD>> http://www.files.thatbelongstome.com/file1 -O test1.xls Next step is to consolidate files from 1 to 10 in a single excel sheet and send to my mail. I am working on... (1 Reply)
Discussion started by: PikK45
1 Replies

2. Shell Programming and Scripting

Convert date in dd mm yyyy format to UNIX timestamp

Hello All, I have a date in DD/MM/YYYY format. I am trying to convert this into unix timestamp. I have tried following: date -d $mydate +%s where mydate = 23/12/2016 00:00:00 I am getting following error: date: extra operand `+%s' Try `date --help' for more information. ... (1 Reply)
Discussion started by: angshuman
1 Replies

3. Shell Programming and Scripting

Convert epoch time stamp into human readable format

Can someone help me to write a shell script to convert epoch timestamp into human readable format 1394553600,"test","79799776.0","19073982.728571","77547576.0","18835699.285714" 1394553600,"test1","80156064.0","19191275.014286","62475360.000000","14200554.720000"... (10 Replies)
Discussion started by: Moon1234
10 Replies

4. Shell Programming and Scripting

Convert epoch to human readable date & time format

Hello I have log file from solaris system which has date field converted by Java application using System.currentTimeMillis() function, example is 1280943608380 which equivalent to GMT: Wed, 04 Aug 2010 17:40:08 GMT. Now I need a function in shell script which will convert 1280943608380... (3 Replies)
Discussion started by: Yaminib
3 Replies

5. AIX

Convert unix timestamp to year month day format ?

Hello, How do I convert unix timestamp value to 'normal' date format - to get year month and day values ? Looks like it's easy to do using GNU date (linux systems). But how do I do tthis on AIX ? I don't want to write C program, any ways to do that using unix shells ? thanks (1 Reply)
Discussion started by: vilius
1 Replies

6. Shell Programming and Scripting

To convert a date(in string format) to unix timestamp

Hi All, I have a string like below. "Mar 31 2009" . I want to convert this to unix time . Also please let me know how to find the unix time for the above string minus one day. For Eg. if i have string "Mar 31 2009" i want to find the unix time stamp of "Mar 30 2009". Thanks in advance,... (11 Replies)
Discussion started by: girish.raos
11 Replies

7. UNIX for Dummies Questions & Answers

Unix timestamp to readable date

How would I convert a unix timestamp such as "1232144092" to a readable date such as "1/16/2009 10:14:28 PM" ? I thought I could use date, but I don't think so now.. Any help would be great!! (4 Replies)
Discussion started by: Rhije
4 Replies

8. Shell Programming and Scripting

convert unix date to readable format

Dear Experts, I need your help to convert a unix date and time format number in to readable format like dd/mm/yyyy . I have a text file of more than 10,000 records and it is like NAME DATE1 COUNTRY DATE2 ABD 1223580395699 USA 1223580395699... (3 Replies)
Discussion started by: shary
3 Replies

9. Programming

converting unix timestamp into readable format using c++

hi everyone, im new here and am in desperate need of help. I want to convert my 32 bit unix time stamp ' 45d732f6' into a readable format (Sat, 17 February 2007 16:53:10 UTC) using c++. I have looked around the interent but i just cant make sense of anything. All examples i can find just... (3 Replies)
Discussion started by: uselessprog
3 Replies

10. UNIX for Dummies Questions & Answers

To convert multi format file to a readable ascii format

Hi I have a file which has ascii , binary, binary decimal coded,decimal & hexadecimal data with lot of special characters (like öƒ.ƒ.„İİ¡Š·œƒ.„İİ¡Š· ) in it. I want to standardize the file into ASCII format & later use that as source . Can any one suggest a way a logic to convert such... (5 Replies)
Discussion started by: gaur.deepti
5 Replies
Login or Register to Ask a Question