linux - AWK convert minutes and seconds to only seconds -


i have file i'm trying convert seconds. looks this:

49.80 1:03.40 1:01.00 1:00.40 1:01.00 

i've tried:

awk -f: '{seconds=($1*60);seconds=seconds+$2;print seconds}' file 

which outputs:

2988 63.4 61 60.4 61 

i've tried:

sed 's/^/:/g' file | awk -f: '{seconds=($2*60);seconds=seconds+$3;print seconds}' 

which outputs same results. obtain these results:

49.80 63.4 61 60.4 61 

just add check if records contains both seconds , minutes

awk -f: 'nf==1; nf==2{seconds=($1*60);seconds=seconds+$2;print seconds}' 
  • nf number of fields in each record(row).

  • nf==1 if contains 1 field, not gonna calculations. see there no {action} part associated check. hence awk performs default action print entire line

  • nf==2 true if string contains 2 fields. takes associated action, performs calculations.

test

$ awk -f: 'nf==1; nf==2{seconds=($1*60);seconds=seconds+$2;print seconds}' file 49.80 63.4 61 60.4 61 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -