parallel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Replacement strings


From: Ole Tange
Subject: Re: Replacement strings
Date: Thu, 26 Jun 2014 10:13:52 +0200

On Thu, Jun 26, 2014 at 2:20 AM, Ole Tange <ole@tange.dk> wrote:
> On Wed, Jun 25, 2014 at 11:53 PM, Ole Tange <ole@tange.dk> wrote:
>
>> So instead it could be {{perl expression}}. I don't think the {{...}}
>> is something you would otherwise write on the command line.
>
> The very basic functionality now works:
>
> # {}
> parallel echo '{{}}' ::: 1/2.3.4.5 3/4.6.7.8
> # {/}
> parallel echo '{{s:.*/::}}' ::: ab.bc/cd.de
> # {//}
> parallel echo '{{s:/[^/]+$::}}' ::: i/ab.bc/cd.de
> # {/.}
> parallel echo '{{s:.*/([^/]+)\.[^/.]+$:$1:}}' ::: i/ab.bc/cd.de
> # {.}
> parallel echo '{{s:\.[^/.]+$::}}' ::: i/ab.bc/cd.de
:
> I had to choose whether the expression is the return value or whether
> the return value is in $_. I have chosen the latter as that is how
> rename(1) also works.
:
> Alternatively the s/// should return $_ like this:
>
>   parallel echo '{{s/\.[^.]+$//; $_}}' ::: 1/2.3.4.5 3/4.6.7.8

which is similar to Perl's 'map { ... } @array'.

> It is easy to change the behaviour (line 6191), so maybe we will just
> see which one is better.

The more I think of this, the more I like the idea. All the existing
replacement strings can be expressed:

        {#} = {{ $_=$self->seq() }}
        {%} = {{ $_=$self->slot() }}
        {} = {{ $_=$_ }}
        {/} = {{ s:.*/:: }}
        {//} = {{ s:/[^/]+$:: }}
        {/.} = {{ s:.*/::; s:\.[^/.]+$::; }}
        {.} = {{ s:\.[^/.]+$:: }}

Or as map{...} style:

        {#} = {{ $self->seq() }}
        {%} = {{ $self->slot() }}
        {} = {{ $_ }}
        {/} = {{ s:.*/::; $_ }}
        {//} = {{ s:/[^/]+$::; $_ }}
        {/.} = {{ s:.*/::; s:\.[^/.]+$::; $_ }}
        {.} = {{ s:\.[^/.]+$::; $_ }}

And I do not see a reason why you should not be able to define your
own replacement strings (and possibly have them in a config file).
Maybe with a syntax like:

  # add 10
  parallel --rpl {+} '{{ $_+=10 }}' -n2 echo {1+} {2+} ::: 10 20
  # remove 2 extensions
  parallel --rpl {..} '{{ s/(\.[^.]+){2}$// }}' echo {..} ::: dir/file.ext1.ext2

This way the replacement strings in GNU Parallel are simply the
predefined functions listed above.

It even seems fairly easy to implement with one exception: --xargs, -m
and -X. For those I need a fast function for computing the length of
the command line if adding another argument. This is to fill up the
line with arguments, but not go over the max length. Maybe there is a
solution to that, too.

I have yet to measure the performance penalty by doing it the {{...}}
way instead of the way with fixed replacement string. My guess is that
it will be slower, but for flexibility you often sacrifice speed.

> I do not see how a similar positional argument can be made

But how about just having the positional number as the first in the {{...}}:

parallel -n4 echo {1} '{{2 $_.="foo"}}' {-1} ::: a b c d


/Ole



reply via email to

[Prev in Thread] Current Thread [Next in Thread]