cat MailingList | cut -d, -f1 | sort -u | wc -l
An example command is shown above. The output would be something like:
name1
name2
2
I’d like the output from the sort to go to the terminal as well as to the pipe.
I’ve used tee before, but I want all output to go to the terminal. I did some googling and scanned the bash doc at tldp which lend me down the path of trying a combination of parenthesis around the commands (subshell) and file descriptor redirecting. I haven’t been successful. Any thoughts?
Thank you.
Can’t be done using bash redirections alone, you need a process to do the splitting.
Try:
cut -d, -f1 < MailingList | sort -u | tee /dev/stdout | wc -l
I corrected that common tendency which you exemplified, to start a pipe with cat from a file.
cut -d, -f1 < MailingList | sort -u | tee >(wc -l)
So redirect the output of the tee to a subshell, that’s one I totally missed. Argh. A subshell is definitely needed?
I was reading things that were swapping, or is that duplicating, file descriptors like: 3>&2 2>&1 1>&3. If tee didn’t exist, is it possible? :\
Using cat is a bad habit that keeps creeping. Redirection, pipes, file descriptors, subshells and their environment, is something I’m working on getting better at.
Thanks all.
You could
cut -d, -f1 < MailingList | sort -u | tee /dev/stderr | wc -l
Isn’t exactly the same, since stdout isn’t going to the terminal. But if you are only worried about it arriving to the terminal, no matter if from stdout or stderr, then it works.
And you can also
exec 3>&1; cut -d, -f1 < pendientes | sort -u | tee /dev/fd/3 | wc -l; exec 3>&-
Once again what goes to the terminal isn’t stdout but the file descriptor #3. But in this case the output will not be mixed with stderr.
Tee must be used yes or yes. The ‘|’ redirects the stdout to the pipe, so stdout doesn’t goes to the terminal anymore. If you want it to go to two different places you need two copies, that’s what tee does.
Your “3>&2 2>&1 1>&3” puts stdout in stderr and stderr in stdout. But since stderr is empty…