singhabsk
singhabsk

Reputation: 31

How can I convert this timestamp into seconds?

I have a file xyz.txt containing the following information:

19-10-13-404566 1-3039 1 xyz
19-10-14-890768 1-3039 2 zxv
..........................

I want the first column of the file converted into 19*3600 + 10*60 + 13 (68473) in Perl.

Upvotes: 0

Views: 510

Answers (3)

dams
dams

Reputation: 399

perl -pi -e 's/^(\d+)-(\d+)-(\d+)/$1*3600+$2*60+$3/e' xyz.txt

That would replace the first column, right in the file.

Upvotes: 0

jrockway
jrockway

Reputation: 42674

perl -ne 's/^(\d+)-(\d+)-(\d+)/$1*3600+$2*60+$3/e; print'

But BTW, your math is wrong for the example. 19 * 3600 + 10 * 60 + 13 is 69013.

Upvotes: 2

Dave Cross
Dave Cross

Reputation: 69244

From what you've written, it's impossible to work out how the transformation you're describing is supposed to work. But this entry from the Perl FAQ may help you to actually change the contents of your file.

How do I change, delete, or insert a line in a file, or append to the beginning of a file?

Upvotes: 0

Related Questions