1

I have a program that I am timing, lets say some_bin. I run the time command and it produces output like so:

time -p some_bin --some-args=args
real 1.09
user 1.08
sys 0.00

I want to get just the 1.09 real time the program was in use. I'm trying to use awk in this case to pattern match the first line on real and then extract the time.

Everything I've tried thus far however has failed to work. Any ideas on how I can accomplish this?

moesef
  • 4,641
  • 16
  • 51
  • 68

2 Answers2

1

Both these command would provide the real time on its own

/usr/bin/time -f "%e" MYCOMMAND

TIMEFORMAT='%2R'; time MYCOMMAND

TIMEFORMAT also allows you to change the precision of the time output.

example output

/usr/bin/time -f "%e" sleep 2
2.00

.

TIMEFORMAT='%2R'; time sleep 2
2.00
Community
  • 1
  • 1
0

time command's output comes after completion of your command and it is written on stderr. You can use it like this:

( time -p some_bin --some-args=args ) |& awk '/real/{print $2}'

You can also use:

( time -p some_bin --some-args=args ) 2>&1 |  awk '/real/{print $2}'
anubhava
  • 761,203
  • 64
  • 569
  • 643