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!


In reply to Converting multiple unix timestamps by Maire

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.