One of the features that makes UNIX so flexible is the ability to combine two or more utilities together to perform a more complex function. Pipes are the mechanisms that allow you to combine several utilities together.
Pipes let you take the standard output of one command and use it for the standard input for another command. The vertical bar (|) is used to designate a pipe and is placed between the two commands.
The alternative to using pipes is the use of temporary files. For example, the piped command:
news | more
news > temp
more temp
rm temp
Standard input and standard output are two concepts used extensively in UNIX. Standard input is a file where a program can get its input. Since the terminal you are sitting at is considered a file, standard input can be your terminal, a disk file, or even output from another program.
Similarly, standard output is a file where a program's output can be sent. Since your terminal is considered a file, standard output can be the terminal or a disk file.
Many commands will use standard input and standard output if a filename is not specified as an argument. The sort command is an example of this. If you just type:
sort
The man page for a command will tell you where it expects to find its input and to send its output.
Some commands can use standard input and standard output simultaneously when they are executed. These commands are ideally suited for use in pipes, and are called filters. An example of a filter is the tr utility. The tr command has the form:
tr string1 string2
cat file1 | tr '[A-Z]' '[a-z]' | more
Try the following commands as examples of pipes. See if you can predict the output before running them. Notice how more complicated functions can be constructed from simpler commands. The last example uses a datafile called namesfile, which you can copy to your current directory using the netscape "Save to File" command.
ls | wc
who | sort
grep -i joe namesfile | sort +1
ls | wc
who | sort
grep -i ...
This file will leave the following file in your current directory: