This article is about the usage of pipe command in the Raspberry Pi Linux system.
Using Pipe Command
To pipeline multiple commands using pipe, follow the below-mentioned syntax:
Below we will see some examples of using pipe command. But before starting let’s suppose we have a file named example-file2 and the content of the file can be displayed using the cat command:
Sorting Data Using Pipe
In the above file, the data is sorted and to arrange the data alphabetically, we will use the below-mentioned command:
What’s happening here is that the output of the file “example-file2” becomes the input result for the sort command.
Sorting and Saving the Output to a New File
If the user wants to save the sorted file to another file, then this can be performed by using the below command:
Syntax
Example
In the file, the sorted data of “example-file2” is stored into a new file, which is named as sorted-file, and this all is done in a single command:
To verify our claimed results here, we have used cat command to display the data stored in sorted-file:
Picking out the Required Data
pipe can also be used to pick the output few contents from a file. For instance, if a user wants to pick 8 initial terms from a file, he/she can follow the below-mentioned command:
Syntax
Example
Note: This number 8 can vary in command according to user requirement.
The head command picks out the first 8 contents from a file.
Just like the head command, the tail command can also be used with a pipe to display the content from the end of the file. In the below example, we are displaying the last 2 names using the tail command:
Syntax
Example
Piping the List Commands
pipe command can also be used with list commands. Below we have shared some examples of list commands where the pipe is used.
Example 1
In the first example of the list command, we will display the total number of files present in the system using the list command:
In the output, the total number of files is displayed.
Example 2
In this list example, we will list all the outputs by using the “more” command along with the pipe command:
As a result of the above command, all the outputs will be displayed on the screen.
Multi-piping
It is not mandatory to use pipe only once in a command instead it can be used multiple times. For instance, a few examples are shared below where the pipe is used more than once.
Example 1
In the below example we will sort first sort our file then after sorting, the first 8 names will be displayed:
Syntax
Example
Note: The number 8 can be replaced with other numbers if a user wants.
Example 2
In this example, I have created a new file and the contents of which are displayed in the below image:
Now to search the number of times a word is repeated in a file, follow the below-mentioned pipe command:
Syntax
Example
In this example the word “banana” is searched through the search-file and the word count of banana in the file is displayed as shown in the image below:
That’s it for this guide!
Conclusion
The pipe command can be used to pipeline multiple commands together. We have shared multiple scenarios in the above guidelines where a pipe command can be used. Go through these commands and execute them by creating your own file so that you can learn the usage of pipe commands on the Raspberry Pi system.