Pipes and Piping

Sometimes the thing that makes the most sense is to feed the output from one command directly into another command without having to resort to files in between at every step of the way. This is called piping. The symbolism is not that subtle: Imagine pieces of pipe connecting one command with another. Not until you run out of pipe does the command's output emerge. The pipe symbol is the broken vertical bar on your keyboard, usually located just below or (depending on the keyboard) just above the <Enter> key and sharing space with the backslash key. Here's how it works:

 cat random_names | sort | wc  w > num_names 

In the preceding example, the output from the cat command is piped into sort, whose output is then piped into the wc command (that's word count). The -w flag tells wc to count the number of words in random_names. So far, so good.

That cat at the beginning is actually redundant, but I wanted to stack up a few commands for you to give you an idea of the power of piping. Ordinarily, I would write that command as follows:

 sort random_names | wc  w > num_names 

The cat is extraneous because sort incorporates its function. Using pipes is a great time saver because you don't always need to have output at every step of the way.

Moving to Linux(c) Kiss the Blue Screen of Death Goodbye!
Moving to Linux: Kiss the Blue Screen of Death Goodbye!
ISBN: 0321159985
EAN: 2147483647
Year: 2005
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net