Raspberry Pi

Using Pipe Command – Raspberry Pi Linux

pipe command (|) is the widely used command for pipelining the outputs of multiple commands together. Using a pipe command, the output of the previous command is pipelined to become the input for the next command. Piping is important while creating a complex workflow as this will help users connect multiple commands together.

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:

$ command1 | command2 | ... | final command

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:

$ cat <file_name>

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:

$ cat example-file2 | sort

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

$ cat <file name> | sort > <new file name to store data>

Example

$ cat example-file2 | sort > sorted-file

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:

$ cat 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

$ cat <file name> | head -8

Example

$ cat sorted-file | head -8

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

$ cat <filename> | tail -2

Example

$ cat sorted-file | tail -2

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:

$ ls | wc -l

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:

$ ls -al | more

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

$ cat <file name> | sort | head -8

Example

$ cat example-file2 | sort | head -8

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

$ cat <file name> | grep search-word | wc -l

Example

$ cat search-file | grep banana | wc -l

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.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.