AhmadRg
AhmadRg

Reputation: 43

GNU Parallel with several variables

I just started using gnu-parallel. I am trying to feed parallel with several variables. Suppose I have a text file (foo.txt) with the content below,

> cat foo.txt
a b c
d e f

Now, if I run the below command,

> range=$(eval echo {10..15})
> parallel -a foo.txt --colsep=' ' echo {} {#} {1} {2} {3} ::: $range

instead of getting,

10 1 a b c
11 2 a b c
12 3 a b c
13 4 a b c
14 5 a b c
15 6 a b c
10 7 d e f
11 8 d e f
12 9 d e f
14 11 d e f
15 12 d e f
13 10 d e f

I get this,

a b c 10 1 a b c
a b c 11 2 a b c
a b c 12 3 a b c
a b c 13 4 a b c
a b c 14 5 a b c
a b c 15 6 a b c
d e f 10 7 d e f
d e f 11 8 d e f
d e f 12 9 d e f
d e f 14 11 d e f
d e f 15 12 d e f
d e f 13 10 d e f

How can I do it right?

Upvotes: 2

Views: 629

Answers (1)

Shawn
Shawn

Reputation: 52344

{} is the complete input line, which are generated by doing a Cartesian product of all input sources - the lines of the file and the command line arguments. If you just want the number portion of each line, it ends up being the fourth column, which you'd have to refer to like you do the other columns:

$ range=( {10..15} )
$ parallel -a foo.txt --colsep=' ' echo {4} {#} {1} {2} {3} ::: "${range[@]}"
10 1 a b c
11 2 a b c
12 3 a b c
13 4 a b c
14 5 a b c
15 6 a b c
10 7 d e f
11 8 d e f
12 9 d e f
13 10 d e f
14 11 d e f
15 12 d e f

Also note using an array for the range variable to avoid that ugly eval echo.

Upvotes: 5

Related Questions