Skip to content

Instantly share code, notes, and snippets.

@brendano
Last active October 20, 2015 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendano/2a90765581e88c8b1b16 to your computer and use it in GitHub Desktop.
Save brendano/2a90765581e88c8b1b16 to your computer and use it in GitHub Desktop.
munge H:M:S and M:S into seconds for zsh time command with ruby regexes
For example for data looking like
qwerasdvzxcasdf 0.62s user 18.55s system 92% cpu 20.678 total
asdfasdf 838.56s user 10.75s system 100% cpu 14:08.98 total
acvzxcvzxcv 3:15:12.2 total
asdfadsf 0.22s user 6.24s system 0% cpu 16:01.30 total
output those 4 numbers in seconds
20.678
848.98
11712.2
961.3
COMMAND:
ruby -ne 'puts $_[/(\S+) total/,1].sub(/^(\d+):(\d+):(\S+)/){ ($1.to_i*3600 + $2.to_i*60 + $3.to_f).to_s }.sub(/^(\d+):(\S+)/){ ($1.to_i*60 + $2.to_f).to_s }'
ruby code expanded to multiple lines. First it tries to match the H:M:S format, then the M:S format.
s = "12:10:4.1"
puts s.sub(/^(\d+):(\d+):(\S+)/){
($1.to_i*3600 + $2.to_i*60 + $3.to_f).to_s
}.sub(/^(\d+):(\S+)/){
($1.to_i*60 + $2.to_f).to_s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment