0

I have two commands say cmd1 and cmd2, wherein i perform

time cmd1 | cmd2

I want to get something like

cmd1 >> file.out and {time cmd1 >> file.out} | cmd2 >> file.out

so can someone suggest how it is actually done? edit: as Anthony's answer below suggests, tee works here but if i write

time cmd1 |tee -a file.out | cmd2 >> file.out

then it only writes the output of cmd1 to file.out and cmd2 to file.out, whereas i also want the output of {time cmd1} to that file.

I am using bash shell on Ubuntu Mate. If the time keyword complicates it, please suggest some method to time the execution and do the exact operation.

Vivek
  • 5
  • 4
  • 3
    Possible duplicate of [how to redirect output of multiple commands to one file](http://stackoverflow.com/questions/20355264/how-to-redirect-output-of-multiple-commands-to-one-file) – Matt S Apr 18 '16 at 14:13
  • Is it a duplicate? That question has two commands, i am asking how to get the output of before the pipe and after the pipe to a file. – Vivek Apr 18 '16 at 14:19

1 Answers1

1

If I understand your question correctly, you want the output of cmd to be written to file.out and also used as the input to cmd2. For this case, you could try inserting the tee command (with the -a option to append) into your command pipeline:

cmd1 | tee -a file.out | cmd2 >> file.out

Example

$ printf "one\ntwo\nthree\n" | tee -a file.out | sed 's/.*/\U&/' >> file.out

$ cat file.out
one
two
three
ONE
TWO
THREE

Answer to edited version of the question

The following construct should do what you want:

{ time cmd1; }  2>> file.out | tee -a file.out | cmd2 >> file.out

Since the time utility provided by Bash operates on the complete pipeline, curly braces are used to group these commands so that they can be considered as a whole. Note: the terminating semi-colon (;) is required before the closing brace.

The standard out stream of cmd1 is piped through to the tee command but since Bash’s time utility prints its timing statistics to standard error, the file descriptor 2 is redirected so that the timing statistics are appended to file.out.

Modified version of previous example

{ time printf "one\ntwo\nthree\n"; }  2>> file.out | tee -a file.out | sed 's/.*/\U&/' >> file.out
Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56
  • This is what I wanted. I found about tee but wasn't sure how to use it together. Let me try this :) – Vivek Apr 18 '16 at 14:25
  • There is a slight twist though. I have edited the question again. Can you help me with that? – Vivek Apr 18 '16 at 14:31
  • @Vivek That's a substantially different question. The use of the `time` command complicates things. I'd also advise editing the question to indicate what shell you're using. Since, you're using Linux, `time` is most likely provided by the Bash shell as a *shell keword*. – Anthony Geoghegan Apr 18 '16 at 14:47
  • Yes its a bash shell on Ubuntu Mate. I have edited now the question. – Vivek Apr 18 '16 at 14:52
  • @Vivek I've updated my answer to answer the revised question – though you should try to describe your question as completely as possible before posting it. – Anthony Geoghegan Apr 18 '16 at 15:22
  • Thank you @Anthony. I wasn't aware that having `time` in cmd1 would complicate the thing so much, so I wrote the question differently the first time. – Vivek Apr 19 '16 at 08:36
  • @Vivek No worries. Asking – and answering – questions is a learning experience. If this answer worked for you, you should [accept it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) by clicking on the check mark to the left. This marks the question as *answered* and (with upvotes) is the way thanks are expressed on the Stack Exchange sites. On the other hand, you could wait and see if someone else comes along with a more useful answer. Welcome to the Stack Exchange network! – Anthony Geoghegan Apr 19 '16 at 08:44