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

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

Replies are listed 'Best First'.
Re^6: Epoch time conversion script
by hedkandi (Initiate) on Apr 05, 2012 at 06:53 UTC

    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

        Hi anonymous monk Thank you for pointing that out. Here's the result again

        # more perl_epoch open my $in, '<', 'jabadm_history' or die "Bummer: $!"; open my $out, '>', 'jabadm.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; } } # ls -lrt jabadm_history -rw-r--r-- 1 jabadm sapsys 1518 Mar 27 13:31 jabadm_history # ./perl_epoch ./perl_epoch: line 1: open: command not found ./perl_epoch: line 2: open: command not found ./perl_epoch: line 4: syntax error near unexpected token `)' ./perl_epoch: line 4: `while(my $line = <$in>){'