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:
|
|
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:
|
|
Count Number of Words of the File Contents
|
|
Count Number of Characters of the File Contents
|
|
Continuous Pipes
Bash supports multiple pipes run back-to-back:
|
|
References 3.2.2 Pipelines
Author Dong Chen
LastMod Fri Feb 22 2019