Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
~$ /usr/bin/time -f "%e: " echo test
test
0.00: 

I want the output to be

0.00: test

How would I accomplish this?

share|improve this question

migrated from stackoverflow.com Jun 22 '12 at 23:45

This question came from our site for professional and enthusiast programmers.

    
This doesn't make sense. You don't know how long the command took until after it's performed all of its output. –  Neil Apr 20 '12 at 22:49
    
/usr/bin/time -f "%e: " echo test >so 2>se; echo $(<se) $(<so) Works. He redirected to file descriptor. –  HaltingState Apr 20 '12 at 23:43

3 Answers 3

As geekosaur's answer says, time prints to stderr, and your command prints to stdout. And time waits for your command to finish before writing its output. I think you should try the program sponge from the package moreutils, like this:

/usr/bin/time -f "%e: " echo test | sponge

Sponge is a program which soaks up all the input and writes it when the input is closed (i.e. the previous program terminates). The above works because time will print to stderr when echo finishes, and echo's stdout will be soaked up by the sponge, which will only wring itself out when the left hand side finishes. So this does pretty much what you want as far as I can tell, though I wasn't able to test it for complete accuracy because my system's /usr/bin/time doesn't support the -f option.

share|improve this answer
    
Our build system might freak out if it sees sterror. –  HaltingState Apr 20 '12 at 23:44
    
You can add redirection to stdout at the end: (/usr/bin/time echo test | sponge) 2>&1 –  John Zwinck Apr 21 '12 at 8:00

I don't think that's possible. One way of doing it is to capture the output into a variable and manipulate it.

share|improve this answer
1  
I did not ask whether it was possible, I asked how to do it. –  HaltingState Apr 21 '12 at 0:24

Unless you're asking for some form of time travel, you would need to capture the output and rearrange it. time outputs its result on standard error.

$ /usr/bin/time -f "%e: " echo test >so 2>se; echo $(<se) $(<so)
0.01: test
share|improve this answer
1  
You are a shell ninja. Thanks. –  HaltingState Apr 20 '12 at 22:53
    
How would I do this in a make file? I wanted to wrap "CXX = g++" –  HaltingState Apr 21 '12 at 0:30
    
That's actually a rather different question, as g++ will send any output (since it only produces output on error) to stderr. It's complex enough that you would want to use a wrapper script. Also, are you thinking of wrapping the (normally nonexistent) output from g++, or the echoed command from make? The latter can't easily be done while echoing the actual command. –  geekosaur Apr 21 '12 at 0:39
    
Anything. I just realized that the output was not from gcc, but from the make file. You are correct. When it is compiling main.cpp it outputs "main.cpp", how would I do "0:04: main.cpp", something like that? –  HaltingState Apr 21 '12 at 0:53
1  
That's more complicated, as the macro doesn't have ready access to the filename you want to echo; you need to replace the make pattern rules. See gnu.org/software/make/manual/make.html#Pattern-Rules for gmake; it's similar for other versions of make although specific details will differ. –  geekosaur Apr 21 '12 at 1:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.