Linux: xargs with multiple parameters
xargs is an awesome command, that can be used to pass input from stdin as parameters to other commands, also xargs can do other tricks like…
xargs is an awesome command, that can be used to pass input from stdin as parameters to other commands, also xargs can do other tricks like run commands in parallel etc, in this example, i will show you how to pass multiple parameters to a command.
Create a file with parameters
Create the following file and save it as params.csv
1 2
3 4
5 6
7 8
9 10Use the -n parameter
Now enter the following, xargs get the parameter from each line, $0 is the first parameter, and the second is $1
cat params.csv| xargs -n2 bash -c 'echo "$0"+"$1"'
1+2
3+4
5+6
7+8
9+10What if the parameters are not space-delimited? xargs in Linux (in macOS not) supports the -d parameter, allowing you to define the delimiter between parameters. Assuming that the parameters file is like this
1,2
3,4
5,6
7,8
9,10Run xargs with the -d parameter like this
cat params.csv| xargs -d ',' -n2 bash -c 'echo "$0"+"$1"'
1+2
3+4
5+6
7+8
9+10Conclusion
xargs is one of the most useful commands, it can leverage simple commands to multiprocessing tools! if you want to do a favor to your self learn as many tricks as you can with xargs and this trick is handy!, I hope you enjoyed this short article!