Home >Operation and Maintenance >Linux Operation and Maintenance >What is the pipe character in linux
In Linux, the pipe character is "|", which is mainly used to connect two or more commands together and use the output of one command as the input of the next command; the syntax is "command1 | command2 [ | commandN... ]", the output of the command on the left side of the "|" character will be used as the input of the command on the right side of the "|" character. The pipe character can be used continuously. The output of the first command will be used as the input of the second command, and the output of the second command will be used as the input of the third command, and so on.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
Shell also has a function, which is to connect two or more commands (programs or processes) together, and use the output of one command as the input of the next command. The two connected in this way Or multiple commands form a pipeline.
Linux pipes use vertical bars|
to connect multiple commands, which is called the pipe character.
command1 | command2 command1 | command2 [ | commandN... ]
When a pipe is set between two commands, the output of the command on the left of the pipe symbol | becomes the input of the command on the right. As long as the first command writes to standard output and the second command reads from standard input, the two commands can form a pipe. Most Linux commands can be used to form pipes.
The pipe character can be used continuously. The output of the first command will be used as the input of the second command, and the output of the second command will be used as the input of the third command, and so on.
It should be noted here that command1 must have correct output, and command2 must be able to process the output result of command2; and command2 can only process the correct output result of command1, not Handle the error message of command1.
For example: sort the hello.sh file and find the line containing "better" after sorting and deduplicating it.
The command is: cat hello.sh | sort | uniq | grep 'better'
[1] The first step - view the text
First use the cat command to view the text, and the content printed on the screen is the output result of the cat command
[root@linuxforliuhj test]# cat hello.sh hello this is linux be better be better i am lhj hello this is linux i am lhj i am lhj be better i am lhj have a nice day have a nice day hello this is linux hello this is linux have a nice day zzzzzzzzzzzzzz dddddddd gggggggggggggggggggg [root@linuxforliuhj test]#
[2] The second process - sorting
Throw the results output by the previous cat command to the sort command through the pipeline, so the sort command sorts the text output by the previous cat command
[root@linuxforliuhj test]# cat hello.sh | sort be better be better be better dddddddd gggggggggggggggggggg have a nice day have a nice day have a nice day hello this is linux hello this is linux hello this is linux hello this is linux i am lhj i am lhj i am lhj i am lhj zzzzzzzzzzzzzz [root@linuxforliuhj test]#
[3] The third process - deduplication
As mentioned in the previous article introducing uniq, sort can be effectively deduplicated when used in combination with uniq, so sort is processed through the pipeline and output The text is thrown to uniq for processing, so uniq processes the sorted text, which can effectively remove duplicates
[root@linuxforliuhj test]# cat hello.sh | sort | uniq be better dddddddd gggggggggggggggggggg have a nice day hello this is linux i am lhj zzzzzzzzzzzzzz [root@linuxforliuhj test]#
[4] The fourth step - filtering
The last step of filtering is the same It is to filter the text output after processing the previous command, that is, the uniq command.
[root@linuxforliuhj test]# cat hello.sh | sort | uniq | grep 'better' be better [root@linuxforliuhj test]#
Here comes the important point!
Here comes the important point!
Here’s the point!
The above cat, sort, uniq, grep and other commands all support the pipe character because these commands can be read from the standard input. The text to be processed (that is, reading parameters from the standard input); for some commands, such as rm, kill and other commands, do not support reading parameters from the standard input, but only support reading parameters from the command line (that is, the rm command The file or directory to be deleted must be specified later, and the process number to be killed must be specified after the kill command.)
So what kind of commands support pipes, and what kind of commands do not support pipes? ?
Generally, commands that process text, such as sort, uniq, grep, awk, sed, etc., all support pipes; commands that do not process text, such as rm and ls, do not support pipes.
[root@linuxforliuhj test]# cat hello.sh | sort be better be better be better dddddddd gggggggggggggggggggg have a nice day have a nice day have a nice day hello this is linux hello this is linux hello this is linux hello this is linux i am lhj i am lhj i am lhj i am lhj zzzzzzzzzzzzzz [root@linuxforliuhj test]#
When there are no parameters after sort, the output of the previous command thrown to it by the pipe character is processed (that is, the standard output of the previous command is used as the standard input of this command)
[root@linuxforliuhj test]# ls beifen.txt hello.sh mk read.ln read.sh read.txt sub.sh [root@linuxforliuhj test]# ls | grep read.sh read.sh [root@linuxforliuhj test]# ls | grep read.sh | rm rm: missing operand Try 'rm --help' for more information. [root@linuxforliuhj test]#
When If the deleted file is not specified after rm, an error will be reported as missing parameters. Therefore, commands such as rm do not support reading parameters from the standard input. They only support specifying parameters on the command line, that is, specifying the deleted file.
There are the following two files
[root@linuxforliuhj test]# cat a.txt aaaa dddd cccc bbbb [root@linuxforliuhj test]# cat b.txt 1111 3333 4444 2222 [root@linuxforliuhj test]#
Execute the command: cat a.txt | sort
[root@linuxforliuhj test]# cat a.txt | sort aaaa bbbb cccc dddd [root@linuxforliuhj test]#
When the command line parameter of sort is empty, the former is used by default The output result of a command is used as the input of this command.
Execute the command: cat a.txt | sort b.txt
[root@linuxforliuhj test]# cat a.txt | sort b.txt 1111 2222 3333 4444 [root@linuxforliuhj test]#
You can see that when the command line parameters of sort (here are b.txt) is not empty, sort will not read the parameters in the standard input, but will read the command line parameters
Execute the command: cat a.txt | sort b.txt -
[root@linuxforliuhj test]# cat a.txt | sort b.txt - 1111 2222 3333 4444 aaaa bbbb cccc dddd [root@linuxforliuhj test]#
" - "means standard input, that is, the output of the command cat a.txt, which is equivalent to sorting the file b.txt and standard input together, which is equivalent to sort a.txt b.txt
[root@linuxforliuhj test]# sort a.txt b.txt 1111 2222 3333 4444 aaaa bbbb cccc dddd [root@linuxforliuhj test]#
思考:对于rm、kill等命令,我们写脚本时常常会遇到需要查询某个进程的进程号然后杀掉该进程,查找某个文件然后删除它这样的需求,该怎么办呢?那就用xargs吧!
相关推荐:《Linux视频教程》
The above is the detailed content of What is the pipe character in linux. For more information, please follow other related articles on the PHP Chinese website!