I know that to capture a pipeline’s contents at an intermediate stage of processing, we use tee as ls /bin /usr/bin | sort | uniq | tee abc.txt | grep out , but what if i don’t want to redirect the contents after uniq to abc.txt but to screen(through stdout, ofcourse) so that as an end result , i’ll have on screen, the intermediate contents after uniq as well as the contents after grep.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
sometimes /dev/tty can be used for that…
ls /bin /usr/bin | sort | uniq | tee /dev/tty | grep out | wc
Method 2
ls /bin /usr/bin | sort | uniq | tee /dev/fd/2 | grep out | wc
On a linux system you can use the the /dev/fd/[num] links like named pipes in many cases. This will duplicate stdout to stderr, which, typically, is your terminal screen, but doesn’t need to be.
Method 3
This command worked for me.
ls /bin /usr/bin | sort | uniq | tee /dev/pts/0 | grep out
You could check what is your terminal using the command tty and replace the tee to redirect the output to that terminal.
References
https://stackoverflow.com/a/18025293/1742825
Method 4
How to do it (example):
exec 3>&1; ( ls |( tee >&3 ) >/dev/null ); exec 3>&-
Which will show the ls result and send it to nirvanah.
To grok the key part, 3>&1, you may read I/O Redirection and esp. this example.
In short: >somefileis short for 1>somefile, which in turn means Assign the file handle of somefile to file descriptor 1 (and drop the former value of that descriptor, for the scope of this process).
So, 3>&1 means: Assign the file descriptor 1 (which may but need not be tty) to the (until now unused) file descriptor 3. We’re effectively using &3 as a temporary variable.
Method 5
mkfifo myfifo cat myfifo& ls /bin /usr/bin | sort | uniq | tee myfifo | grep out
mkfifo creates a FIFO (first in, first out) special file, a.k.a. a named pipe.
Start an asynchronous cat to read from the fifo, and then run your pipeline,
teeing the intermediate result to the fifo.
This will produce a [1]+ Done cat myfifo message at the end.
You can suppress that with this magic trick:
(cat myfifo&); ls /bin /usr/bin | sort | uniq | tee myfifo | grep out
For a long term, robust solution, you might want to create a permanent fifo
(e.g., $HOME/myfifo) rather than creating a new one every time.
But that will fail if you may be running multiple instances of this simultaneously.
Alternatively,
- Generate a unique name (e.g., with
mktemp). - Create the fifo in a directory that’s guaranteed to be writable (e.g.,
/tmp). - Remove the fifo at the end of the command.
Method 6
In the case this is being run from a terminal you could
- start a terminal multiplexer such as tmux or screen
- split your terminal – in tmux Ctrl B"
- in the first window
touch abc.txtthentail -f abc.txt - switch to the second window and run your command. You’ll see the file created by tee update.
Method 7
http://tldp.org/LDP/abs/html/io-redirection.html
cat stuff | 3>&1 tee 3 | xsel -i
Edit: unfortunately, the above ↑ is incorrect.
An earlier answer of
cat stuff | tee >(cat) | xsel -i
is good though.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0