Maire has asked for the wisdom of the Perl Monks concerning the following question:
Good afternoon monks,
I have a very large .txt file containing approximately 800,000 lines of text. Each line, at some point, contains at least two timestamps which are currently in Unix time. Below are examples of two lines from my data:
Misc data that is unimportant:false,true,false,"retrieved_on":14335074 +59,more misc data,nul;,true,"created_utc":"1433122764", "retrieved_on":1433507472,misc data,"created_utc":"1433122764",true,fa +lse,more misc data
I am trying to convert all of the Unix timestamps to a more traditional date and time format using the following code:
use strict; use utf8; open(FILE, '<C:/Users/li/perl/part1.txt') || die "File not found"; my @lines = <FILE>; close(FILE); my @newlines; foreach(@lines) { $_ =~ s/([\d]{10})/localtime $1/eg; push(@newlines,$_); } open(FILE, '>C:/Users/li/perl/part1_CORRECTED.txt') || die "File not f +ound"; print FILE @newlines; close(FILE);
However, while this code does, technically work, it returns the date and time in a strange format which is not helpful to when my analysing my data further:
Misc data that is unimportant:false,true,false,"retrieved_on":Fri Jun + 5 13:30:59 2015,more misc data,nul;,true,"created_utc":"Mon Jun 1 0 +2:39:24 2015", "retrieved_on":Fri Jun 5 13:31:12 2015,misc data,"created_utc":"Mon J +un 1 02:39:24 2015",true,false,more misc data
Is there a way that I can modify my code to return the output in a more intuitive style (e.g. maybe YYYY:MM:DD HH:MM:SS or similar)?
Thank you in advance!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Converting multiple unix timestamps
by Tux (Canon) on Jul 27, 2018 at 14:40 UTC | |
by Maire (Scribe) on Jul 27, 2018 at 14:50 UTC | |
|
Re: Converting multiple unix timestamps
by hippo (Archbishop) on Jul 27, 2018 at 14:34 UTC | |
by Maire (Scribe) on Jul 27, 2018 at 14:49 UTC | |
|
Re: Converting multiple unix timestamps
by Laurent_R (Canon) on Jul 27, 2018 at 19:19 UTC | |
by Maire (Scribe) on Aug 02, 2018 at 09:33 UTC | |
|
Re: Converting multiple unix timestamps -- oneliner
by Discipulus (Canon) on Jul 28, 2018 at 10:44 UTC | |
by Laurent_R (Canon) on Jul 28, 2018 at 18:16 UTC |