Home > Blockchain >  Unix converting time format to integer value
Unix converting time format to integer value

Time:10-09

I have the following text file.

Account1,2h 01m 00s
Account2,4h 25m 23s
Account3,5h 43m 59s

I wish to add the values of hours, minutes and seconds in order to total them to their respective minute totals.

Account1 minute total = 121
Account2 minute total = 265
Account3 minute total = 343

I have the following bash file

cat data.txt | cut -f2 -d',' 

This isolates the time values; however, from here I don't know what steps I would take to isolate the time, convert it to integers and then convert it to minutes. I have tried using a PARAM but to no avail.

CodePudding user response:

If awk is an option, you can try this

awk -F"[, ]" '{h=60; m=1; s=0.01666667}{split($2,a,/h/); split($3,b,/m/); split($4,c,/s/); print$1, "minute total = " int(a[1] * h   b[1] * m   c[1] * s)}' input_file
$ cat awk.script

BEGIN {
  FS=",| "
} {
  h=60
  m=1
  s=0.01666667
}{
  split($2,a,/h/)
  split($3,b,/m/)
  split($4,c,/s/)
  print $1, "minute total = " int(a[1] * h   b[1] * m   c[1] * s)
}

Output

awk -f awk.script input_file
Account1 minute total = 121
Account2 minute total = 265
Account3 minute total = 343
  • Related