To complete this chapter, you can do the following things:
Understand the purpose of the pipeline
Create a pipe to get output from one command and then manufacture input for another command
Using Tee,cut,tr,more, and PR filters
10.1 Pipe Introduction
The shell provides the ability to connect commands through a pipe. The flexibility to reflect the operating environment of the UNIX system is fully filtered through the files. You can use pipelines to filter the output of a command.
This chapter introduces the use of pipes, and some filters (Cut,tr,tee and PR) so that you can further process the output of a file or command.
10.2 Why Use Pipelines
Do not use pipelines
$ who > Temp_file
$ wc-l < Temp_file
$ RM Temp_file
&http://www.aliyun.com/zixun/aggregation/37954.html ">NBSP; who | Wc-l
If you use I/O redirection to complete the filtering of the output of a command, you will first redirect the output of a command to a temporary file and then filter the contents of the temporary file, and when you do so, you have to delete the temporary file. Although this approach extends the ability to command, it is not convenient to delete temporary files.
The pipe lets you direct the output of one command to the input of another, you do not need to create intermediate files, so you do not need to purge when you complete the operation. This is the flexible and powerful embodiment of UNIX systems: commands and commands can be linked together to improve the processing power of a single command line.
10.3 ' | ' symbol
The function of the "|" symbol (read as a pipe character) is to join two commands. The standard output of the command to the left of the pipe character is used as the standard input to the command on the right side of the pipe, and the command appearing in the middle of the pipe must be able to receive standard input and output to standard output.
Filters such as Wc,sort, and grep can receive standard input and produce standard output, so they can appear in the middle of a pipe. By linking commands and filters together, you can perform complex operations.
The following commands are required for each location of the pipeline:
Any command on the left of a | symbol must be able to output to standard output.
Any in | The command on the right side of the symbol must be able to read data from standard input.
Any in two | The commands between symbols must be able to receive standard input and be able to produce output to standard output. (is a filter)
More command
The more command is used to display the contents of the file on the screen, one screen at a time. The more command can also read standard input. Therefore, it can appear on the right side of a pipe to control the output of any command and produce output to standard output. If the output of the command is very long (one screen is not complete), you can use the more command to achieve one screen browsing function at a time.