[PDC - Center for Parallel Computers, KTH]

An Introduction to Using Pipes in AIX


Table of Contents


What are Pipes?

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:

could be simulated with: Pipes save you the bother of creating and removing the temporary file.


Standard Input & Standard Output

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:

the sort command will wait for you to type in the lines that you want sorted. Sort is waiting for input from standard input, which in this case would be your terminal. After you have typed in the lines to be sorted, sort will send the sorted output back to standard ouput, which is your terminal.

The man page for a command will tell you where it expects to find its input and to send its output.


Filters

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 takes standard input and translates all characters in string 1 to the corresponding characters in string 2. The command below would convert all uppercase characters in file1 to lowercase. The output is then viewed a screenful at a time with more.

General Approach

  1. To use pipes, you must have at least two commands: one that sends its output to standard output for the first, and one that reads its input from standard input for the second. You can connect more than two commands with a pipe. Usually the middle components of the pipe are filters.
  2. Type the commands in the order you want them executed, separating each command from the next with the vertical bar, |.
  3. Press the return key, and your commands will be executed.

Examples

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.


Resultant Output


Cleanup

This file will leave the following file in your current directory:


References


[Copyright Statement] [Feedback]

Changed by:$Author: kinsella $,$Date: 1999/07/08 07:35:59 $