Unix Pipes Tutorial: Pipes in Unix Programming

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 9, 2024

Overview of Pipes in Unix Programming:

In this tutorial, we will learn more about Unix Pipes.

And later, we will work with some of the remaining filter commands and see an example of piping them together.

Pipes in Unix Programming

Unix Video #20: 

Pipes in Unix

A series of filter commands can be piped together using the pipe symbol: ‘|’. When two commands are piped together, the stdin of the second program is read from the stdout of the first program. This creates a powerful mechanism for running complex commands quickly.

Commandsort: this command is used to sort the contents of the file. This command is also useful to merge the sorted files and store the result in some file. The contents of the original file remain unaltered.
Common Syntax:sort[OPTION]…[FILE]
Example1:sort file1
This command will sort the contents of file1
Example2:sort -o output_file file1 file2
This will sort the contents of file1 and file2 and save the result in output_file file.
Commandcut – this command is used to cut a given number of characters or columns from a file. For cutting a certain number of columns it is important to specify the delimiter. A delimiter specifies how the columns are separated in a text file e.g. number of spaces, tabs or other special characters.
Common Syntax: cut OPTION …[FILE]
Example 1 cut -c 5-10 file1
It will cut 5 to 10 characters from each line of file1
Example 2cut -d “,“ -f2,6 file1
This will cut 2nd and 6th fields from file1, where the fields are separated by delimiter “,”

This will cut 2nd and 6th  fields from file1, where the fields are separated by the delimiter “,”.

Let us now see an Example of using pipes to print out a sorted list of unique words. If file1 has a list of words in a random order with random repetitions, then the following piping can be used to achieve this.

$ sort file1 | uniq > file2

Here, the sort command reads input from the file ‘file1’ and sends the output to stdout. The pipe symbol causes the output of the sort command to be redirected to the input of the uniq command. The uniq commands reads the sorted list from its stdin and prints the unique words from there to its stdout.

Finally, the output redirection symbol ‘>’ redirects the stdout of the uniq command to the file ‘file2’.

Hope you enjoyed this tutorial. Next we will cover the remaining filter commands.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

Leave a Comment