◆ Function: reformat the data received from the standard input, and then provide it as a parameter to other commands.
◆ Example:
◆ Convert the command output to one line for display
[Root @ web2] # megacli-pdlist-aall | grep-e 'slot number: | raw size: | firmware state: '| awk' {print $3} '| xargs
4 286102 MB unconfigured (good) 5 286102 MB unconfigured (good) 0 140014 mb online 1 140014 MB online 2 140014 MB online 3 140014 MB online
◆ Convert one line of output content into multiple lines for display
[Root @ web2] # Cat dell_diskstats.txt | awk '{print $3}' | xargs-N 3 | sort
0 140014 MB online
1 140014 MB online
2 140014 MB online
3 140014 MB online
4 286102 MB unconfigured (good)
5 286102 MB unconfigured (good)
◆ Convert input into a column of parameters
Tom @ Ubuntu: $ cat example.txt
1 2 3 4 5
6 7 8 9 10
Tom @ Ubuntu: $ cat example.txt | xargs
1 2 3 4 5 6 7 8 9 10
◆ Converting single-row input to multi-row output (-N)
Tom @ Ubuntu: $ cat example.txt | xargs-N 2
1 2
3 4
5 6
7 8
9 10
◆ Use custom delimiters to separate parameters (-d)
Tom @ Ubuntu: $ echo "tomxisxaxboy." | xargs-D x
Tom is a boy.
◆ Read stdin and pass the formatting parameters to the command
The following is the content of the executable script to display each of its parameters:
Tom @ Ubuntu: $ cat cecho. Sh
#! /Bin/bash
# File name: cecho. Sh
# Description: This shell shows how to use xargs.
Echo $ *'#'
Tom @ Ubuntu: $./cecho. Sh arg1 arg2 arg3
Arg1 arg2 arg3 #
The following is an example of argS usage:
Tom @ Ubuntu: $ cat args.txt | xargs-N 1./cecho. Sh
Arg1 #
Arg2 #
Arg3 #
Tom @ Ubuntu: $ cat args.txt | xargs-N 2./cecho. Sh
Arg1 arg2 #
Arg3 #
You can see through the following method that this is one or multiple executions of a parameter with a maximum number (-N specified:
Tom @ Ubuntu: $ cat args.txt | xargs-N 2 bash-X./cecho. Sh
+ Echo arg1 arg2 '#' ---- the first command to be executed
Arg1 arg2 #
+ Echo arg3 '#' ---- the second command executed
Arg3 #
The following is an execution with three parameters:
Tom @ Ubuntu: $ cat args.txt | xargs bash-X./cecho. Sh
+ Echo arg1 arg2 arg3 '#'
Arg1 arg2 arg3 #
If there are multiple parameters and only one parameter changes, you can use the xargs-I type selection to specify the replacement string, the implementation command executes each parameter once (the result is visible, and the result is that each parameter is executed once ):
Tom @ Ubuntu: $ cat args.txt | xargs-I {} bash-X./cecho. Sh-P {}-I
+ Echo-P arg1-I '#' ---- Replace with the first parameter and run it once.
-P arg1-I #
+ Echo-P arg2-I '#' ---- replace it with the second parameter and run it once.
-P arg2-I #
+ Echo-P arg3-I '#' ---- replace it with the third parameter and run it once.
-P arg3-I #
◆ Use xargs for Statistics
Count the number of file lines, for example, count the number of rows of all. c files in the source directory.
Tom @ Ubuntu: $ find.-Type F-name "*. c"-print0 | xargs-0 WC-l
4./test2.c
4./test3.c
16./file_oper.c
Total usage
-Print0 uses \ 0 as the terminator of each result found by find. That is, the result found by FIND is separated by \ 0 rather than \ n.-0 is used to identify the demarcation mark of \ 0.