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

Hi netwallah

I am trying to convert the epoch time to local time. I can use a one-liner that does the job but it's tedious to do it manually with many users on the system

cat /var/log/user_history/32adm_bash_history | while read line ; do i +f [[ $line =~ '^#' ]]; then date -d "@$(echo $line | cut -c2-)"; els +e echo $line ; fi; done

Replies are listed 'Best First'.
Re^3: Epoch time conversion script
by Anonymous Monk on Mar 28, 2012 at 07:15 UTC

    Could you please post some sample lines from 32adm_bash_history so we can see the format of your file? And what your expected output would look like?

      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
        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; } }
        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)