GNU parallel combinatorics, using the argument list multiple times

I would like to use the following to create unique tasks, where {1} and {2} are the only tuples:

parallel echo {1} {2} ::: ABCD ::: ABCD 

For example, python (itertools) provides such a combinatorial generator:

 permutations('ABCD', 2) 

AB AC AD BA BC BD CA CB CD DA DB DC


Is there a way to implement it directly through bash? Or is GNU parallel? Maybe skip extra tasks? But then, how can I check which combinations of parameters have already been used.

 parallel echo {= 'if($_==3) { skip() }' =} ::: {1..5} 
+5
source share
3 answers

If the values ​​are unique:

 parallel echo {= 'if($arg[1] eq $arg[2]) { skip() }' =} ::: ABCD ::: ABCD 

Or more generally:

 parallel echo \ '{= my %seen; for my $a (@arg) { $seen{$a}++ and skip() } =}' \ ::: ABCD ::: ABCD ::: ABCD 

If you want to treat AB as BA , then this is only one of the following combinations:

 parallel echo \ '{= for my $t (2..$#arg) { if($arg[$t-1] ge $arg[$t]) { skip() } } =}' \ ::: ABCD ::: ABCD ::: ABCD 

If you use these a lot, remember that you can use --rpl to create your own replacement strings by putting this in ~ / .parallel / config

 --rpl '{unique} my %seen; for my $a (@arg) { $seen{$a}++ and skip() }' --rpl '{choose_k} for my $t (2..$#arg) { if($arg[$t-1] ge $arg[$t]) { skip() } }' 

And then run:

 parallel echo {unique} ::: ABCD ::: ABCD ::: ABCD parallel echo {choose_k} ::: ABCD ::: ABCD ::: ABCD 
+2
source

One ugly solution: use Python to generate the sequence and the --link (or --xapply ) --xapply :

 $ parallel --xapply echo {1} {2} ::: $(python -c "from itertools import permutations ; print(' ::: '.join([' '.join(_) for _ in zip(*list(permutations('ABCD',2)))]))") AB AC AD BA BC BD CA CB CD DA DB DC 
0
source

You can use parallel twice if you don't mind:

 # filter the first parallel output for accepted combinations and pipe into a 2nd parallel parallel echo {1} {2} ::: abc ::: abc | awk '{ if ($1 != $2) {print $0}}' | parallel echo this is the actual {1} {2} # no one-liner for better maintenance parallel echo {1} {2} ::: abc ::: abc | awk '{ if ($1 != $2) {print $0}}' > myargs parallel --arg-file myargs echo {1} {2} rm myargs # Output is in both cases ab ac ba bc ca cb 
0
source

Source: https://habr.com/ru/post/1271931/


All Articles