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.
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.
Command | sort: 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. |
Command | cut – 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 2 | cut -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.