In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to use find, xargs, grep and pipes". The editor shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to use find, xargs, grep and pipes" can help you solve the problem.
Question:
I'm sure you all know the command to search for files containing fixed strings in the directory:
Find. -name'* .py'| xargs grep test
At first, I was not familiar with the xargs command, so the command I used directly was
Find. -name'* .py'| grep test
The result is not what you expected. This command is just to find out if the file name * .txt has test.
Here I'll take a look at exactly what xargs has done to make the results different.
Parameters and standard input:
These two words are common in Linux commands. But there is actually a difference between parameters and standard input. Many of the commands we use every day, such as ls-lah. Medium. L, a, h,. Are all parameters to the command ls. As for standard input, it can be said that it is some kind of stream data. Generally speaking, the stream data source of standard input is our terminal input. In the Linux command, some commands can accept standard input and some cannot. Like the ls above, you can only receive parameters, not standard input. Like the cat command or the echo command, these are fine.
How to tell whether a command can receive standard input? Very simply, when you type the command enter, the terminal will wait to receive your input, for example, when you enter cat in the terminal, the terminal will wait for you to enter characters, when you enter some characters, and then press Ctrl-C to send the termination symbol. At this point, the cat command receives the standard input and executes the command, that is, the standard output (screen) of what you just typed.
Pipe:
The function of the pipe is to use the standard output of the previous command as the standard input of the later command. Note here that the latter command receives standard input, so if the command does not support receiving standard input, you cannot directly use pipes. For example, the commonly used ls command can only use parameters, not standard input, so [command] | ls cannot be used. Commands such as echo or cat are fine. Then there must be a way to combine these commands with pipes that cannot be used with standard input, and then xargs comes out.
Xargs command:
Generally speaking, the xargs command converts standard input into various formatted parameters, so the command [command 1] | xargs [command 2] is to turn the standard output of command 1 into standard input of xargs through pipeline, and then xargs changes this standard input into a parameter and passes it to [command 2]. In this way, with the xargs command, we can use commands that do not receive standard input behind the pipe. For example, [command 1] | xargs ls, are you familiar with it?
Find and grep:
With the above knowledge, we can finally answer the first question here. Why the order?
Find. -name'* .py'| grep test
And
Find. -name'* .py'| xargs grep test
The result is different.
Let's first take a look at the grep manual. Through the man grep command.
DESCRIPTION
Grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
You can see here that grep supports standard input.
Suppose the following files exist in the directory:
$ls
Altitest.py python.py runora.py test.py TransferFile.py
So for the first command, find. -name'* .py'| grep test, which passes the standard output of the previous command to grep test as standard input, so grep looks for test characters from these standard inputs, that is, the character stream composed of file names.
$find. -name'* .py'| grep test
. / Altitest.py
. / test.py
You can see that these file names are finally selected.
For the second command, find. -name'* .py'| xargs grep test. The file name obtained through xargs,find becomes the parameter passed to the following grep. Then these file names are the actual file identifiers. After receiving them, grep will search for strings in each file in the normal way.
# find. -name'* .py'| xargs grep test
. / runora.py:testConn = cx_Oracle.connect ('user/passwd@tns')
. / runora.py:testCursor = testConn.cursor ()
. / runora.py:testCursor.execute ("select * from table")
. / runora.py:rows = testCursor.fetchall ()
. / runora.py:testCursor.close ()
. / runora.py:testConn.close ()
Here is a clear understanding of the role of find,grep,xargs and pipeline.
Xargs also has the function of specifying the location of parameters. Suppose we want to put all the .py files in the directory into the Python directory, using the command
Find. -name'* .py'| xargs-I {} mv {}. / Python
The parameter-I specifies where the command before the pipe should be located behind the pipe as a parameter.
When we look at many command manuals, the manual explains how to use the command. For example
Grep [OPTIONS] PATTERN [FILE...], that is, the last location of the command is the file name [FILE].
Note here that the file name [FILE] is also a parameter.
That's all for "how to use find, xargs, grep and pipes". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.