in reply to Re^3: Epoch time conversion script
in thread Epoch time conversion script

Hi anonymous This is the sample history file with hashed Epoch time in seconds taken from a different user:

[root@H99A105 user_history]# tail -f sapr3_history #+1331257705 Date is: Fri Mar 9 10:48:25 JST 2012 #+1331257706 history

After the conversion it should look like this:

[root@H99A105 user_history]# tail -f /var/log/user_history/Epoch_Conve +rt/sapr3 Fri Mar 9 10:48:25 JST 2012 Date is: Fri Mar 9 10:48:25 JST 2012 Fri Mar 9 10:48:26 JST 2012 history

Replies are listed 'Best First'.
Re^5: Epoch time conversion script
by lidden (Curate) on Mar 28, 2012 at 10:36 UTC
    Something like this should do what you want.

    open my $in, '<', 'infile.txt' or die "Bummer: $!"; open my $out, '>', 'outfile.txt' or die "What: $!"; while(my $line = <$in>){ if(my ($epoch) = $line =~ /^#\+(\d+)\s*$/){ my $human_readable = `date -d \@$epoch`; # or: # my $human_readable = scalar localtime $epoch; print $out $human_readable; } else{ print $out $line; } }

      Hi lidden I used the while loop you provided and I'm getting this error

      [root@H99A115 03]# ./convert ./convert: line 4: syntax error near unexpected token `{' ./convert: line 4: `while(my $line = $in){'

      I removed the <> tags from <$in> and still the same error this is my input and output file:

      open my $in, '<', 'q22adm_history' or die "Bummer: $!"; open my $out, '>', 'q22adm' or die "What: $!";

        Well, the while loop lidden provided has no syntax errors, so show your code

        I removed the <> tags from <$in>

        that had nothing to do with the error, that was reading the file, that is readline

Re^5: Epoch time conversion script
by NetWallah (Canon) on Mar 28, 2012 at 17:34 UTC
    One liner (Using Windows style quotes. Change to Single quote for linux):
    perl -pe "m/^#\+(\d+)/ and $_=scalar localtime $1" InputFileName > Ou +tputFileName

                 All great truths begin as blasphemies.
                       ― George Bernard Shaw, writer, Nobel laureate (1856-1950)

      Hi netwallah You're a genius! This totally solved my issue. Im going to add this to cron rightaway! Thanks!

      Ok I should probably add also that it works for C shell users only! But thank you nevertheless!