Home > database >  Get xargs to allow multiple arguments to command
Get xargs to allow multiple arguments to command

Time:02-15

I've got a file of arguments:

file.txt:

1 2 3
a b c
4 5 6

I want to pass them to a command that takes 3 arguments. As a minimal example, xargs will parse a command as a single string if you use "-I" directly:

echo '1 2 3' | xargs -I@ python -c "import sys; print(sys.argv)" @              
>>> ['-c', '1 2 3']

Strangely, the $(echo ...) trick doesn't work:

echo '1 2 3' | xargs -I@ python -c "import sys; print(sys.argv)" $(echo @)
>>> ['-c', '1 2 3']

Even though a string literal in the same place would:

echo '1 2 3' | xargs -I@ python -c "import sys; print(sys.argv)" $(echo '1 2 3')
>>> ['-c', '1', '2', '3']

CodePudding user response:

xargs -n 3 cmd

Executes cmd, with a maximum of three arguments at a time.

For example:

xargs -n 3 echo < file.txt
# gives
1 2 3
a b c
4 5 6

xargs -n 2 echo < file.txt
# gives
1 2
3 a
b c
4 5
6
  • Related