Introduction
We can use the pipe to "standard output" of one command as the "standard input" of another command, but the standard output here is the data flow, but the input of some commands only receives the parameter command does not receive the data flow, then this general method cannot implement, here need to use the Xargs command. Xargs can change the data flow to a command parameter as a standard input parameter, and then learn more about xargs usage.
Multi-line conversions
[email protected] test]# cat test 0 1 2 3 4 5 6 7 8 9
There are 0-9 of the two rows of records in the current test table, separated by a space between each number
1. Single-line display, default Xargs replaces newline characters with spaces
[email protected] test]# cat test| 0123456789
2. Multi-line display, using the-n number parameter
The number value indicates how many parameters are displayed per row and numbers must be greater than 0, and if number is greater than the parameter, the maximum number of parameters is displayed, for example, one parameter per row.
1
delimiting delimiter
Xargs can split the text into multiple parts with a single character as a delimiter of text, but the separator can only be a single character and cannot be a combined character .
1. Split by character
" 111a222a333a444 " |xargs-d A
2. Can also be split into multi-line display with the-n parameter
" 111a222a333a444 " 2
Command-pass parameter
Next you can see how xargs the standard output data flow to command parameters
Next, take a simple bash example
# !/bin/bash " End "
This parameter takes a parameter and returns the original argument with end as the end of the line
It can be seen from the xargs that the standard output is passed as a parameter to the Bashtest
Other usage
You can use-I replace-str when you pass a command that has multiple parameters and only needs to pass arguments to a single parameter
Vim Bashtest
# !/bin/bashecho $1 $2"end"
I just need to pass the parameters of the Xargs to the
1 |xargs-i str ./bashtest STR A
Note: The names of the replace-str on both sides must be consistent and not limited in name.
Summary
Xargs the use of a lot of skills, are interested to go slowly groping
Note: pursuer.chen Blog:http://www.cnblogs.com/chenmh This site all the essays are original, welcome to reprint, but reprint must indicate the source of the article, and at the beginning of the article clearly give the link. Welcome to the exchange of discussions |
Linux xargs switch output Data flow to command parameters