21. Pipelines - Use Other Commands' Running Results
Contents
With the pipe notation, we can pass the output of one program to the current as its initial data.
Pipe Notation (|)
A Scenario-based Question
Let’s assume there’s a file called fresh_file that holds two lines of text:
|
|

Print fresh_file content
This file has only two lines of text, so the number of lines is noticeable, but what if you encounter a text file with a lot of content? How can we quickly and efficiently get the number of lines of a text file?
Count Number of Lines of the File Contents
We can pass the output of the cat command to the wc command through the pipeline feature of the shell to treat it as the initial data of the wc command:
|
|

Pass the printed output to wc command to count the number of lines of the file content
Count Number of Words of the File Contents
|
|

Pass the printed output to wc command to count the number of words of the file content
Count Number of Characters of the File Contents
|
|

Pass the printed output to wc command to count the number of characters of the file content
Continuous Pipes
Bash supports multiple pipes run back-to-back:
|
|

The pipeline will execute these commands from left to the right one by one. Every subsequent command must be able to receive the results of the previous command
References 3.2.2 Pipelines
Author Dong Chen
LastMod Fri Feb 22 2019