Question

I have file whose looks like something below

 16sec
 16sec
 1mins,36sec
 19mins,34sec
 43sec
 6mins,38sec

I want convert these values to seconds so that I will get an output like below

 16
 16
 96
 1174
 43
 398

How can I achieve this through linux shell script

Was it helpful?

Solution

Perl version:

#!/usr/bin/perl

use warnings;
use strict;

use 5.010;
use feature "switch";

use Data::Dumper;

while (<DATA>) {
    chomp;
    if (m/^(?:(\d+)mins,)?(\d+)sec$/) {
        my $min = $1 ? $1 : 0;
        my $sec = $2 ? $2 : 0;
        print $min * 60 + $sec, "\n";
    }
}

__DATA__
16sec
16sec
1mins,36sec
19mins,34sec
43sec
6mins,38sec
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top