How can I print only the real time output from the `time` command?

0
3
Asked By CleverCactus93 On

I'm trying to run a command using `time`, like `time ./prog`, and I want to keep the output minimal. Specifically, I only want to capture the first line that shows the 'real' time, like `real 0m0.004s` or just `0m0.004s`. Is there a simple way to achieve this?

3 Answers

Answered By QuickFox84 On

You can customize the output to only show the real time using the `TIMEFORMAT` variable in bash. For example, you can set it like this: `TIMEFORMAT="real %3lR"; time ./prog`. This will limit the output to just the real time in your desired format. Just remember that using a proper `time` executable can change the output slightly, so keep that in mind!

Answered By CunningPineapple21 On

Another approach is to use a pipeline with `head`. You can run something like `time ./prog | head -1` to get the first line, which should be the real time line. If you prefer using `sed`, you could also do `time ./prog | sed -n -e '/real/p'` to ensure you capture that line specifically.

Answered By SleekOtter42 On

You have a few options to get just the real time output. The simplest might be using `grep` like this: `time ./prog | grep "real"` to grab the line directly. Alternatively, you can also use `awk` to print it: `time ./prog | awk '/^real/{print $2}'` if you just want the time value without the label.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.