Linux xargs converts output data streams into command parameters, linuxxargs
Introduction
We can use the pipeline to take the "standard output" of a command as the "standard input" of another command. However, the standard output here is a data stream, however, if some command input only receives parameter commands and does not receive data streams, this general method cannot be implemented. xargs commands are required here. Xargs can convert data streams into command parameters as standard input parameters. Next, we will learn more about xargs usage.
Multiline Conversion
[root@localhost test]# cat test0 1 2 3 45 6 7 8 9
There are 0-9 rows of records in the current test table. Each number is separated by a space.
1. Single line display. By default, xargs will replace line breaks with spaces
[root@localhost test]# cat test|xargs 0 1 2 3 4 5 6 7 8 9
2. Multi-line display, using the-n number Parameter
The number value indicates the number of parameters displayed in each line, and the number must be greater than 0. If the number is greater than the number of parameters, it is displayed as the maximum number of parameters. For example, each line displays a parameter.
cat test|xargs -n 1
Separator
Xargs can use a single character as the delimiter for a text segment. The text is divided into multiple parts and separated by spaces. However, the delimiter can only be a single character, not a combination character.
1. Split by characters
echo "111a222a333a444" |xargs -d a
2. You can also split the data into multiple rows based on the-n parameter.
echo "111a222a333a444" |xargs -d a -n 2
Command parameter passing
Next, let's take a look at how xargs converts standard output data streams into command parameters.
Next we will use a simple bash example.
#!/bin/bashecho $* "end"
This parameter receives the parameter and returns the original parameter with end as the end of the row.
From the above, we can see that xargs passed the standard output as a parameter to bashtest.
Other usage
When the passed command has multiple parameters and only needs to pass parameters to a single parameter, you can use-I replace-str
Vim bashtest
#!/bin/bashecho $1 $2 "end"
I only need to pass the xargs parameter to $1
cat test|xargs -n 1 |xargs -I str ./bashtest str a
Note: The replace-str names on both sides must be consistent with each other.
Summary
There are still many skills to use xargs. If you are interested, you can explore it slowly.
Note: Author: pursuer. chen Blog: http://www.cnblogs.com/chenmh All essays on this site are original. You are welcome to repost them. However, you must indicate the source of the article and clearly give the link at the beginning of the article. Welcome to discussion |