11
echo "'param 1' 'param 2'" | xargs -n2 -I@ echo [@] [@]

This command outputs:

[param 1 param 2] [param 1 param 2]

However, I would like it to output:

[param 1] [param 2]

Is there a way to do this with xargs? I plan to use this with -L1 so the solution would handle multiple lines as well as multiple arguments.

Bastardo
  • 4,144
  • 9
  • 41
  • 60
jcalfee314
  • 4,642
  • 8
  • 43
  • 75
  • What exactly are you trying to achieve? Are you trying to 'decorate' each filename argument before executing a command, and to treat them one pair of file names at a time? I would normally write a script for `xargs` to run that does the job - it is far less painful than trying to contort `xargs` into doing what it does not want to do. – Jonathan Leffler May 23 '11 at 15:28

5 Answers5

10

For those who find this from a search, the accepted answer did not work for me.

echo "'param 1' 'param 2'" | xargs -n1 | xargs -I@ echo \[@\] \[@\]

produces:

[param 1] [param 1]
[param 2] [param 2]

which does not meet the requirements given by the original poster to have xargs read in multiple entities, separate them, and send them to a single command ("echo" in the OP) as separate parameters. Xargs is not designed for this sort of task!


The bash answer can work.

p=(`echo "param1 param2"`); echo [${p[0]}] [${p[1]}]

produces:

[param1] [param2]

but this solution does not work with more than one line.


A correct solution with bash for sending pairs of lines as arguments to a single command is:

(echo 'param 1'; echo 'param 2'; echo 'param 3'; echo 'param 4') | while read line1; read line2; do echo "[$line1] [$line2]"; done

produces:

[param 1] [param 2]
[param 3] [param 4]


The GNU Parallel answer does work, but GNU Parallel must be make'd and installed. (The version packaged with Ubuntu is not GNU Parallel.)

Sarkom
  • 823
  • 10
  • 12
  • @wok, can you elaborate? How can you use xargs to produce what I have produced with bash and what is required by the OP? – Sarkom Nov 04 '13 at 03:10
  • Maybe see http://stackoverflow.com/a/19497860/376454 if you want to apply the same treatment to every line of a file. – Wok Nov 04 '13 at 14:23
  • I have tried again, and **for a file** on Linux, I **need** to use `xargs` as shown in my answer. With your answer only, there is no **line break** in the output, just `[param 1] [param 2] [param 3] [param 4]`. Apart from this detail, your answer is very good. – Wok Nov 04 '13 at 14:58
  • @wok, your answer proves that xargs alone is not sufficient to do what the OP requires, and you need a bash solution (like my answer) for yours to work. Please edit your first comment that says my answer is wrong. – Sarkom Nov 05 '13 at 15:58
  • I cannot edit it, but I can remove it. Anyway, your answer does not work without xargs for a .txt file. – Wok Nov 05 '13 at 16:58
  • @wok, Thanks. My answer is intended for the "multiple lines" case mentioned by the OP. Your answer is needed for a different case. I think they are both correct for their own purpose. I wrote my answer because I had a need to process pairs of lines from a .txt file at one time. – Sarkom Nov 06 '13 at 17:30
3
echo "'param 1' 'param 2'" | xargs -n1 | xargs -I@ echo \[@\] \[@\]

(In my shell I need to escape [], your mileage may vary).

UncleZeiv
  • 18,272
  • 7
  • 49
  • 77
  • 2
    How would I separate the first from the second? For example if I wanted to swap the order the 2 parameters? – John Sep 27 '13 at 00:35
1

With GNU Parallel you can do:

(echo 'param 1'; echo 'param 2') | parallel -N2 echo '\[{1}] \[{2}]'

Watch the intro video to learn more: http://www.youtube.com/watch?v=OpaiGYxkSuQ

Ole Tange
  • 31,768
  • 5
  • 86
  • 104
1

why stick xargs? bash could handle this well:

p=(`echo "param1 param2"`); echo ${p[0]} ${p[1]}
lyman
  • 183
  • 1
  • 5
0

As suggested in this answer, given one echo:

param1 param2

param 3 param4

The following pipe:

... | while read line1; read line2; do echo "[$line1] [$line2]"; done

produces:

[param1] [param2]
[param3] [param4]

If you have a file myFile.txt containing many lines with two parameters on each, use the following strategy based on xargs in order to reuse the arguments as $f and $g:

cat myFile.txt | xargs -n1 | while read f; read g; do echo $f $g; done
Community
  • 1
  • 1
Wok
  • 4,956
  • 7
  • 42
  • 64