Most other uses of
cat myfile | are probably not going to prove very clever, especially if you're sending the output to
awk, or other commands that are well prepared to read files without intervening pipes. An exercise that I like to give to my Intro Unix students is to have them rewrite commands such as
cat /etc/passwd | grep $USER: without the pipes. "Sure," I tell them, "pipes are wonderful things. But that doesn't mean you need to use them every chance you get".
Of course, explaining to new Unix users when pipes work and when they don't is much harder than having them rewrite a list of suboptimal commands.
There are commands, such as
date that just aren't going to pay attention to anything that is piped to them and commands --
cd anyway -- that don't generate any output that can be piped to other commands. You can type
ls | ls and get a file listing, but that first
ls is not going to be delivering its output to the second. Similarly,
who | ls is going to give you the same result because
ls isn't looking for content being piped to it.
Knowing when to use a pipe involves becoming familiar enough with each command to know what you can do with them. Commands such as
touch don't generate output, nor do they read and use content that is piped to them. You can issue a command such as
echo hello | touch, but you're not going to create a file named hello. Instead, you're just going to get a
touch: missing file operand-type of error. Other commands, such as
less can both generate output and read content that is piped to them. Then there are commands like
cal that do one (generate output), but not the other (read piped input) of the two pipe functions.
When you think about Unix commands and pipes this way, it's not so surprising that neophytes find the learning curve of Unix a bit steep and might actually try commands such as
who | ls or
tail -1 dates | cd, never mind
cat myfile | wc -l. Once they fully understand when pipes work, when they don't work and when they don't contribute (and are, thus, useless), they'll be ready to work wonders on the command line.
Read more of Sandra Henry-Stocker's Unix as a Second Language blog and follow the latest IT news at ITworld, Twitter, and Facebook.