- Linux Shell Scripting Cookbook(Third Edition)
- Clif Flynt Sarath Lakshman Shantanu Tushar
- 158字
- 2021-07-09 19:46:15
How to do it...
The xargs command supplies arguments to a target command by reformatting the data received through stdin. By default, xargs will execute the echo command. In many respects, the xargs command is similar to the actions performed by the find command's -exec option:
- Converting multiple lines of input to a single-line output:
Xarg's default echo command can be used to convert multiple-line input to single-line text, like this:
$ cat example.txt # Example file 1 2 3 4 5 6 7 8 9 10 11 12 $ cat example.txt | xargs 1 2 3 4 5 6 7 8 9 10 11 12
- Converting single-line into multiple-line output:
The -n argument to xargs limits the number of elements placed on each command line invocation. This recipe splits the input into multiple lines of N items each:
$ cat example.txt | xargs -n 3 1 2 3 4 5 6 7 8 9 10 11 12