Subject: Re: New xargs, was Re: upgrade to mv
To: None <tech-userlevel@netbsd.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-userlevel
Date: 08/16/2001 12:39:17
>> [my own xargs]
>>  *      -k<str> [...]
> That looks really useful!  I've done
> 	commands | xargs -n1 sh -c 'command $2 arg arg'

That's more or less equivalent to find -exec, in that it runs one
command (and a shell) per string.

I've often done stuff like

	mcgrep -l somestring *.txt | xargs -kX mv X somedir

to run commands like

	mv foo.txt bar.txt baz.txt somedir

>>  *      -r<c><s1><c><s2>
> This looks like it could be useful, too, but your explanation is
> giving me a headache.  :-)

That's why there are examples! :-)

	xargs -kXX -r/A/B cmd mAjB blivet A -q XX -f mXX B -t
with input
	foo
	bar
	blee
gives you
	cmd mjjj blivet -q foo -f mfoo -q bar -f mbar -q blee -f mblee -t

Here's how that breaks down.

( echo foo ; echo bar ; echo blee ) | \
	xargs -kXX -r/A/B cmd mAjB blivet A -q XX -f mXX B -t

.-------> cmd
| .----->     mAjB
+-|----->          blivet
| | .--->                 A -q XX -f mXX B
+-|-|--->                                  -t
| | |
`-|-|---These are literals and appear literally in the result.----.
  `-|---This argument contains the -r strings A and B; the part   |
    |   between A and B is replicated, once per argument.-------. |
    `---This is the most complicated piece.  The four args      | |
        between A and B are replicated,-----------------------. | |
        and the XX (the -k string) is replaced by the         | | |
        argument each time.---------------------------------. | | |
                                                            | | | |
                   +-------+------+-------+------+--------+-' | | |
                 +-|-------|----+-|-------|----+-|--------|---' | |
     +++---------|-|-------|----|-|-------|----|-|--------|-----' |
+----|||-+-------|-|-------|----|-|-------|----|-|--------|----+--'
|    ||| |       +-|-------|.   +-|-------|.   +-|--------|-.  |
v    vvv v      /  v       v \ /  v       v \ /  v        v  \ v
cmd mjjj blivet -q foo -f mfoo -q bar -f mbar -q blee -f mblee -t

I no longer recall what need it was that prompted me to add -r, but
there must have been one.

Of course, it's also non-Standard.  In the case of xargs, this doesn't
worry me; the whitespace and quoting botches are bad enough for me to
consider Standard xargs borderline useless.

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML	       mouse@rodents.montreal.qc.ca
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B