Piping – using | symbol

Reading Time: 3 minutes

Pipelines (|)

We often want to take the output of one command and pass it on to another. The standard way of performing this is using a pipeline, referred to as pipe, or pipeing the output.

The pipe is a vertical bar ‘|’. On a standard US keyboard this shares the same ‘\’ key, but is sometimes located above the RETURN key. On a standard UK keyboard this is normally found at the bottom left of the keyboard and is typed by using shift and the ‘\’ key. On other European keyboards this may be on one of the number keys (e.g. Alt-Gr 6).

The first command to run is listed first followed by the pipe symbol and then followed by the second command. Any output from the first command is then used as input to the second command. We can search, sort, slice, dice, and transform data stored in files in many different ways. A pipe (also called a pipeline) is a powerful shell feature that allows you to p ump the output of one program directly into another.

For example to sort a basic directory listing by name the ls command is piped through the sort command.

ls | sort

The output can be passed through a number of commands by using a pipe through each one. The full command string is referred to as a pipeline.

ls | sort | more

In the below example, we use sort and uniq command to sort a file and print unique values.

$ sort record.txt | uniq 

This will sort the given file and print the unique values only.

Use ls and find to list and print all lines matching a particular pattern in matching files. This command select files with .parm extension in the given directory and search for pattern like “program” in the below example and print those which have word table1 in them.

$ ls -l | find ./ -type f -name "*.parm" -exec grep "table1" {} \;

The command below would take the output from the cat command (listing the contents of a file). and send them to the grep command. The grep command searches for each occurrence of the word High in it’s input. The grep command output would now consist of each line within the file that contained the word High. This output is now sent to the wc command where the wc command with the -l option prints the total number of lines contained in it’s input. 

cat sample.text | grep "High" | wc -l