Pergunta

Eu tenho um arquivo com data e hora no formato:

HH:MM:SS.MS

por exemplo.

00: 04: 02.08

00: 04: 01.08

Cada timestamp está em uma linha diferente, geralmente apenas duas linhas em um arquivo

Eu preciso escrever um script awk para calcular média destes tempos. Estou bastante ingênua em awk scripting, então se alguém pode por favor me dê um trecho de código, será de muita ajuda.

Mesmo um script shell (bash) solução vai ajudar.

Foi útil?

Solução


/..:..:..\./ {
  ++count
  split($0,a,":");
  seconds = (a[1] * 60.0 + a[2]) * 60.0 + a[3]
  print $0, seconds
  alltime += seconds
}
END {
  if (count > 0) {
    avgtime = alltime / count
    print count, alltime, avgtime
    mins = int(avgtime / 60.0) % 60
    hours = int(avgtime / 60.0 / 60.0)
    secs = avgtime % 60.0
    printf("%02d:%02d:%05.2f\n", hours, mins, secs)
  }
} 

E executá-lo ...


$ cat test.data
other stuff

    00:04:02.08
more stuff

    00:04:01.08

more stuff
$ awk -f q.awk < test.data
    00:04:02.08 242.08
    00:04:01.08 241.08
2 483.16 241.58
00:04:01.58
$ 

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top