Normally, we get the output of any command into the terminal. Sometimes, we need to send the output of the command as the input of another command. This task can be done by executing the commands multiple times. But this task can be done easily using a pipe command that executes two or more commands at a time in Linux where the output of the one command is the input of the next command. The ‘|’ operator is used for piping. The uses of the pipe command are described in this tutorial.
Examples of Pipe Commands
The different uses of the pipe(|) command are shown in this part of the tutorial.
Example 1: Write to a File
The particular string value can be written to a new file using the “echo” and “cat” commands.
Run the following command to write a string value into the test.txt file. Here, the output of the “echo” command is passed as the input of the “cat” command:
Run the following command to check the content of the test.txt file:
The following output shows that the file is created successfully with the output of the “echo” command:
Example 2: Count the Total Number of Particular Files
The method of counting the total number of text files that start with the word “test” is shown in this example. This task can be done using multiple commands or a single command with the pipe (|).
Run the following command to print the list of text files of the current location:
The following output shows that there are two text files that start with the word “test”. These are test.txt and testfile.txt:
Run the following command to store the list of text files in the $list variable:
Run the following command to find out the list of text files that start with the word “test” and store them in the filter.txt file:
Run the following command to count the total number of lines of the filter.txt file:
The following output appears after executing the previous commands:
The task of the previous commands can be done easily using the following single command where the output of the “ls” command is sent as the input of the “grep” command. The output of the “grep” command is as the input of the “wc” command using pipe (|).
The following output appears after executing the previous command:
Example 3: Sort the Content of a File
The “sort” command can be used to sort the content of the file in different ways. The method of sorting the content of a text file using the “cat” and “sort” commands is shown in this part of the tutorial.
Create a text file named products.txt with the following content:
Run the following command to check the content of the products.txt file:
The following output appears after executing the previous command:
Run the following command to sort the content of the file in ascending order based on the first column of the file:
The following output appears after executing the previous command:
Run the following command to sort the content of the file in descending order based on the first column of the file:
The following output appears after executing the previous command:
Run the following command to sort the content of the file in ascending order based on the third column of the file:
The following output appears after executing the previous command:
Run the following command to sort the content of the file in ascending order based on the first column of the file and store the sorted output in the sortedProduct.txt file:
Run the following command to check the content of the sortedProduct.txt file:
The following output appears after executing the previous commands:
Example 4: Read the Particular Content of a File
The “head” command is used to read a particular number of lines from the beginning of the file. The “tail” command is used to read a particular number of lines from the end of the file. The methods of using the “cat” and “head” commands, “cat” and “tail” commands, and “cat”, “head”, and “tail” commands with the pipe (|) operator are shown in this example.
Run the following commands to check the content of the products.txt file and print the first two lines of the products.txt file:
$ cat products.txt | head -2
The following output appears after executing the previous commands:
Run the following commands to check the content of the products.txt file and print the last three lines of the products.txt file:
$ cat products.txt | tail -3
The following output appears after executing the previous commands:
Run the following commands to check the content of the products.txt file and print the last line from the first three lines of the products.txt file:
$ cat products.txt | head -3 | tail -1
The following output appears after executing the previous commands:
Conclusion
The pipe (|) operator is a very useful operator of Bash which is used for multiple purposes. Some common uses of this operator are shown in this tutorial using various examples. We hope that the use of the pipe (|) operator is cleared for the Bash users after reading this tutorial.