GNU parallel - two parameters from array as parameter Announcing the arrival of Valued...

Project Euler #1 in C++

Strange behavior of Object.defineProperty() in JavaScript

macOS: Name for app shortcut screen found by pinching with thumb and three fingers

Amount of permutations on an NxNxN Rubik's Cube

Time evolution of a Gaussian wave packet, why convert to k-space?

Would it be easier to apply for a UK visa if there is a host family to sponsor for you in going there?

Google .dev domain strangely redirects to https

An adverb for when you're not exaggerating

How many morphisms from 1 to 1+1 can there be?

How to compare two different files line by line in unix?

What's the difference between the capability remove_users and delete_users?

How often does castling occur in grandmaster games?

How does light 'choose' between wave and particle behaviour?

Does the Mueller report show a conspiracy between Russia and the Trump Campaign?

What order were files/directories output in dir?

What's the meaning of "fortified infraction restraint"?

How to report t statistic from R

preposition before coffee

What would you call this weird metallic apparatus that allows you to lift people?

What is the chair depicted in Cesare Maccari's 1889 painting "Cicerone denuncia Catilina"?

What do you call the main part of a joke?

What does Turing mean by this statement?

What is best way to wire a ceiling receptacle in this situation?

Is multiple magic items in one inherently imbalanced?



GNU parallel - two parameters from array as parameter



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionHow to copy in two folders simultaneously using GNU parallel by spawning multiple threads?gnu parallel with no argument scriptcsh array/command substitution with gnu parallelGNU Parallel: immediately display job stderr/stdout one-at-a-time by jobs orderRunning GNU Parallel on 2 or more nodes with Slurm schedulerPrevent GNU parallel from splitting quoted argumentsGNU Parallel for a iteration while loop, nestedgnu parallel pair argument with file input argumentsCan GNU Parallel Alter the Output of a Bash ScriptCopying & Renaming Files with GNU Parallel





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







5















I have a script that uses gnu parallel. I want to pass two parameters for each "iteration"



in serial run I have something like:



for (( i=0; i<=10; i++ ))
do
a = tmp1[$i]
b = tmp2[$i]
done


And I want to make this parallel as



func pf()
{
a=$1
b=$2
}
export -f pf
parallel --jobs 5 --linebuffer pf ::: <what to write here?>









share|improve this question





























    5















    I have a script that uses gnu parallel. I want to pass two parameters for each "iteration"



    in serial run I have something like:



    for (( i=0; i<=10; i++ ))
    do
    a = tmp1[$i]
    b = tmp2[$i]
    done


    And I want to make this parallel as



    func pf()
    {
    a=$1
    b=$2
    }
    export -f pf
    parallel --jobs 5 --linebuffer pf ::: <what to write here?>









    share|improve this question

























      5












      5








      5


      1






      I have a script that uses gnu parallel. I want to pass two parameters for each "iteration"



      in serial run I have something like:



      for (( i=0; i<=10; i++ ))
      do
      a = tmp1[$i]
      b = tmp2[$i]
      done


      And I want to make this parallel as



      func pf()
      {
      a=$1
      b=$2
      }
      export -f pf
      parallel --jobs 5 --linebuffer pf ::: <what to write here?>









      share|improve this question














      I have a script that uses gnu parallel. I want to pass two parameters for each "iteration"



      in serial run I have something like:



      for (( i=0; i<=10; i++ ))
      do
      a = tmp1[$i]
      b = tmp2[$i]
      done


      And I want to make this parallel as



      func pf()
      {
      a=$1
      b=$2
      }
      export -f pf
      parallel --jobs 5 --linebuffer pf ::: <what to write here?>






      linux gnu-parallel






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 30 '17 at 12:59









      Martin PerryMartin Perry

      1607




      1607






















          2 Answers
          2






          active

          oldest

          votes


















          6














          Omitting your other parallel flags just to stay focused...



          parallel --link pf ::: A B ::: C D


          This will run your function first with a=A, b=C followed by a=B, b=D or



          a=A b=C
          a=B b=D


          Without --link you get full combination like this:



          a=A b=C
          a=A b=D
          a=B b=C
          a=B b=D




          Update: As Ole Tange metioned in a comment [since deleted - Ed.] there is another way to do this: use the :::+ operator. However, there is an important difference between the two alternatives if the number of arguments is not the same in each param position. An example will illustrate.



          parallel --link pf ::: A B ::: C D E output:



          a=A b=C
          a=B b=D
          a=A b=E


          parallel pf ::: A B :::+ C D E output:



          a=A b=C
          a=B b=D


          So --link will "wrap" such that all arguments are consumed while :::+ will ignore the extra argument. (In the general case I prefer --link since the alternative is in some sense silently ignoring input. YMMV.)






          share|improve this answer

































            1














            To simplify, I will assume bash and that the arrays are indexed from 1 rather than 0. It seems intuitive to want to do something like this:



            parallel ... pf '$tmp1[{#}]' '$tmp2[{#}]' ::: $(seq 10)


            where the two args to your pf function are part of the command, and we use the parallel notation {#} to stand for the job number (which is set to 1 up to 10 for 10 jobs. We simply use seq to get 10 arguments after the ::: to ensure we do 10 jobs. (The seq values are not used, and just happen to be the same as the job numbers.)



            Unfortunately, this will not work as bash does not export array variables. But it can export functions, and the parallel man page shows the workaround using a simple import_array function to export/import a function of your choice, my_importer that will set the array variables of your choice:



            declare -a tmp1 tmp2
            for (( i=1; i<=10; i++ ))
            do tmp1[$i]=x$i
            tmp2[$i]=y$i
            done

            import_array(){
            local func=$1; shift;
            export $func='() {
            '"$(for arr in $@; do
            declare -p $arr|sed '1s/declare -./&g/'
            done)"'
            }'
            }
            import_array my_importer tmp1 tmp2


            We neeed only tell parallel to pass the my_importer function into the environment of the pf command, with option --env my_importer, and then run that function before running pf:



            pf(){ a=$1; b=$2; echo "job a=$a b=$b"; }
            export -f pf

            parallel -v --jobs 5 --linebuffer
            --env my_importer 'my_importer;' pf '${tmp1[{#}]}' '${tmp2[{#}]}' ::: $(seq 10)


            The resulting output with -v is similar to



            my_importer; pf ${tmp1[2]} ${tmp2[2]}
            my_importer; pf ${tmp1[1]} ${tmp2[1]}
            my_importer; pf ${tmp1[5]} ${tmp2[5]}
            my_importer; pf ${tmp1[3]} ${tmp2[3]}
            job a=x1 b=y1
            my_importer; pf ${tmp1[6]} ${tmp2[6]}
            job a=x2 b=y2
            my_importer; pf ${tmp1[7]} ${tmp2[7]}
            job a=x4 b=y4
            ...





            share|improve this answer



















            • 1





              @meuth You should stop using my_importer and use env_parallel instead.

              – Ole Tange
              Sep 30 '17 at 18:45












            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395298%2fgnu-parallel-two-parameters-from-array-as-parameter%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            6














            Omitting your other parallel flags just to stay focused...



            parallel --link pf ::: A B ::: C D


            This will run your function first with a=A, b=C followed by a=B, b=D or



            a=A b=C
            a=B b=D


            Without --link you get full combination like this:



            a=A b=C
            a=A b=D
            a=B b=C
            a=B b=D




            Update: As Ole Tange metioned in a comment [since deleted - Ed.] there is another way to do this: use the :::+ operator. However, there is an important difference between the two alternatives if the number of arguments is not the same in each param position. An example will illustrate.



            parallel --link pf ::: A B ::: C D E output:



            a=A b=C
            a=B b=D
            a=A b=E


            parallel pf ::: A B :::+ C D E output:



            a=A b=C
            a=B b=D


            So --link will "wrap" such that all arguments are consumed while :::+ will ignore the extra argument. (In the general case I prefer --link since the alternative is in some sense silently ignoring input. YMMV.)






            share|improve this answer






























              6














              Omitting your other parallel flags just to stay focused...



              parallel --link pf ::: A B ::: C D


              This will run your function first with a=A, b=C followed by a=B, b=D or



              a=A b=C
              a=B b=D


              Without --link you get full combination like this:



              a=A b=C
              a=A b=D
              a=B b=C
              a=B b=D




              Update: As Ole Tange metioned in a comment [since deleted - Ed.] there is another way to do this: use the :::+ operator. However, there is an important difference between the two alternatives if the number of arguments is not the same in each param position. An example will illustrate.



              parallel --link pf ::: A B ::: C D E output:



              a=A b=C
              a=B b=D
              a=A b=E


              parallel pf ::: A B :::+ C D E output:



              a=A b=C
              a=B b=D


              So --link will "wrap" such that all arguments are consumed while :::+ will ignore the extra argument. (In the general case I prefer --link since the alternative is in some sense silently ignoring input. YMMV.)






              share|improve this answer




























                6












                6








                6







                Omitting your other parallel flags just to stay focused...



                parallel --link pf ::: A B ::: C D


                This will run your function first with a=A, b=C followed by a=B, b=D or



                a=A b=C
                a=B b=D


                Without --link you get full combination like this:



                a=A b=C
                a=A b=D
                a=B b=C
                a=B b=D




                Update: As Ole Tange metioned in a comment [since deleted - Ed.] there is another way to do this: use the :::+ operator. However, there is an important difference between the two alternatives if the number of arguments is not the same in each param position. An example will illustrate.



                parallel --link pf ::: A B ::: C D E output:



                a=A b=C
                a=B b=D
                a=A b=E


                parallel pf ::: A B :::+ C D E output:



                a=A b=C
                a=B b=D


                So --link will "wrap" such that all arguments are consumed while :::+ will ignore the extra argument. (In the general case I prefer --link since the alternative is in some sense silently ignoring input. YMMV.)






                share|improve this answer















                Omitting your other parallel flags just to stay focused...



                parallel --link pf ::: A B ::: C D


                This will run your function first with a=A, b=C followed by a=B, b=D or



                a=A b=C
                a=B b=D


                Without --link you get full combination like this:



                a=A b=C
                a=A b=D
                a=B b=C
                a=B b=D




                Update: As Ole Tange metioned in a comment [since deleted - Ed.] there is another way to do this: use the :::+ operator. However, there is an important difference between the two alternatives if the number of arguments is not the same in each param position. An example will illustrate.



                parallel --link pf ::: A B ::: C D E output:



                a=A b=C
                a=B b=D
                a=A b=E


                parallel pf ::: A B :::+ C D E output:



                a=A b=C
                a=B b=D


                So --link will "wrap" such that all arguments are consumed while :::+ will ignore the extra argument. (In the general case I prefer --link since the alternative is in some sense silently ignoring input. YMMV.)







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 42 mins ago

























                answered Sep 30 '17 at 13:24









                B LayerB Layer

                4,1241625




                4,1241625

























                    1














                    To simplify, I will assume bash and that the arrays are indexed from 1 rather than 0. It seems intuitive to want to do something like this:



                    parallel ... pf '$tmp1[{#}]' '$tmp2[{#}]' ::: $(seq 10)


                    where the two args to your pf function are part of the command, and we use the parallel notation {#} to stand for the job number (which is set to 1 up to 10 for 10 jobs. We simply use seq to get 10 arguments after the ::: to ensure we do 10 jobs. (The seq values are not used, and just happen to be the same as the job numbers.)



                    Unfortunately, this will not work as bash does not export array variables. But it can export functions, and the parallel man page shows the workaround using a simple import_array function to export/import a function of your choice, my_importer that will set the array variables of your choice:



                    declare -a tmp1 tmp2
                    for (( i=1; i<=10; i++ ))
                    do tmp1[$i]=x$i
                    tmp2[$i]=y$i
                    done

                    import_array(){
                    local func=$1; shift;
                    export $func='() {
                    '"$(for arr in $@; do
                    declare -p $arr|sed '1s/declare -./&g/'
                    done)"'
                    }'
                    }
                    import_array my_importer tmp1 tmp2


                    We neeed only tell parallel to pass the my_importer function into the environment of the pf command, with option --env my_importer, and then run that function before running pf:



                    pf(){ a=$1; b=$2; echo "job a=$a b=$b"; }
                    export -f pf

                    parallel -v --jobs 5 --linebuffer
                    --env my_importer 'my_importer;' pf '${tmp1[{#}]}' '${tmp2[{#}]}' ::: $(seq 10)


                    The resulting output with -v is similar to



                    my_importer; pf ${tmp1[2]} ${tmp2[2]}
                    my_importer; pf ${tmp1[1]} ${tmp2[1]}
                    my_importer; pf ${tmp1[5]} ${tmp2[5]}
                    my_importer; pf ${tmp1[3]} ${tmp2[3]}
                    job a=x1 b=y1
                    my_importer; pf ${tmp1[6]} ${tmp2[6]}
                    job a=x2 b=y2
                    my_importer; pf ${tmp1[7]} ${tmp2[7]}
                    job a=x4 b=y4
                    ...





                    share|improve this answer



















                    • 1





                      @meuth You should stop using my_importer and use env_parallel instead.

                      – Ole Tange
                      Sep 30 '17 at 18:45
















                    1














                    To simplify, I will assume bash and that the arrays are indexed from 1 rather than 0. It seems intuitive to want to do something like this:



                    parallel ... pf '$tmp1[{#}]' '$tmp2[{#}]' ::: $(seq 10)


                    where the two args to your pf function are part of the command, and we use the parallel notation {#} to stand for the job number (which is set to 1 up to 10 for 10 jobs. We simply use seq to get 10 arguments after the ::: to ensure we do 10 jobs. (The seq values are not used, and just happen to be the same as the job numbers.)



                    Unfortunately, this will not work as bash does not export array variables. But it can export functions, and the parallel man page shows the workaround using a simple import_array function to export/import a function of your choice, my_importer that will set the array variables of your choice:



                    declare -a tmp1 tmp2
                    for (( i=1; i<=10; i++ ))
                    do tmp1[$i]=x$i
                    tmp2[$i]=y$i
                    done

                    import_array(){
                    local func=$1; shift;
                    export $func='() {
                    '"$(for arr in $@; do
                    declare -p $arr|sed '1s/declare -./&g/'
                    done)"'
                    }'
                    }
                    import_array my_importer tmp1 tmp2


                    We neeed only tell parallel to pass the my_importer function into the environment of the pf command, with option --env my_importer, and then run that function before running pf:



                    pf(){ a=$1; b=$2; echo "job a=$a b=$b"; }
                    export -f pf

                    parallel -v --jobs 5 --linebuffer
                    --env my_importer 'my_importer;' pf '${tmp1[{#}]}' '${tmp2[{#}]}' ::: $(seq 10)


                    The resulting output with -v is similar to



                    my_importer; pf ${tmp1[2]} ${tmp2[2]}
                    my_importer; pf ${tmp1[1]} ${tmp2[1]}
                    my_importer; pf ${tmp1[5]} ${tmp2[5]}
                    my_importer; pf ${tmp1[3]} ${tmp2[3]}
                    job a=x1 b=y1
                    my_importer; pf ${tmp1[6]} ${tmp2[6]}
                    job a=x2 b=y2
                    my_importer; pf ${tmp1[7]} ${tmp2[7]}
                    job a=x4 b=y4
                    ...





                    share|improve this answer



















                    • 1





                      @meuth You should stop using my_importer and use env_parallel instead.

                      – Ole Tange
                      Sep 30 '17 at 18:45














                    1












                    1








                    1







                    To simplify, I will assume bash and that the arrays are indexed from 1 rather than 0. It seems intuitive to want to do something like this:



                    parallel ... pf '$tmp1[{#}]' '$tmp2[{#}]' ::: $(seq 10)


                    where the two args to your pf function are part of the command, and we use the parallel notation {#} to stand for the job number (which is set to 1 up to 10 for 10 jobs. We simply use seq to get 10 arguments after the ::: to ensure we do 10 jobs. (The seq values are not used, and just happen to be the same as the job numbers.)



                    Unfortunately, this will not work as bash does not export array variables. But it can export functions, and the parallel man page shows the workaround using a simple import_array function to export/import a function of your choice, my_importer that will set the array variables of your choice:



                    declare -a tmp1 tmp2
                    for (( i=1; i<=10; i++ ))
                    do tmp1[$i]=x$i
                    tmp2[$i]=y$i
                    done

                    import_array(){
                    local func=$1; shift;
                    export $func='() {
                    '"$(for arr in $@; do
                    declare -p $arr|sed '1s/declare -./&g/'
                    done)"'
                    }'
                    }
                    import_array my_importer tmp1 tmp2


                    We neeed only tell parallel to pass the my_importer function into the environment of the pf command, with option --env my_importer, and then run that function before running pf:



                    pf(){ a=$1; b=$2; echo "job a=$a b=$b"; }
                    export -f pf

                    parallel -v --jobs 5 --linebuffer
                    --env my_importer 'my_importer;' pf '${tmp1[{#}]}' '${tmp2[{#}]}' ::: $(seq 10)


                    The resulting output with -v is similar to



                    my_importer; pf ${tmp1[2]} ${tmp2[2]}
                    my_importer; pf ${tmp1[1]} ${tmp2[1]}
                    my_importer; pf ${tmp1[5]} ${tmp2[5]}
                    my_importer; pf ${tmp1[3]} ${tmp2[3]}
                    job a=x1 b=y1
                    my_importer; pf ${tmp1[6]} ${tmp2[6]}
                    job a=x2 b=y2
                    my_importer; pf ${tmp1[7]} ${tmp2[7]}
                    job a=x4 b=y4
                    ...





                    share|improve this answer













                    To simplify, I will assume bash and that the arrays are indexed from 1 rather than 0. It seems intuitive to want to do something like this:



                    parallel ... pf '$tmp1[{#}]' '$tmp2[{#}]' ::: $(seq 10)


                    where the two args to your pf function are part of the command, and we use the parallel notation {#} to stand for the job number (which is set to 1 up to 10 for 10 jobs. We simply use seq to get 10 arguments after the ::: to ensure we do 10 jobs. (The seq values are not used, and just happen to be the same as the job numbers.)



                    Unfortunately, this will not work as bash does not export array variables. But it can export functions, and the parallel man page shows the workaround using a simple import_array function to export/import a function of your choice, my_importer that will set the array variables of your choice:



                    declare -a tmp1 tmp2
                    for (( i=1; i<=10; i++ ))
                    do tmp1[$i]=x$i
                    tmp2[$i]=y$i
                    done

                    import_array(){
                    local func=$1; shift;
                    export $func='() {
                    '"$(for arr in $@; do
                    declare -p $arr|sed '1s/declare -./&g/'
                    done)"'
                    }'
                    }
                    import_array my_importer tmp1 tmp2


                    We neeed only tell parallel to pass the my_importer function into the environment of the pf command, with option --env my_importer, and then run that function before running pf:



                    pf(){ a=$1; b=$2; echo "job a=$a b=$b"; }
                    export -f pf

                    parallel -v --jobs 5 --linebuffer
                    --env my_importer 'my_importer;' pf '${tmp1[{#}]}' '${tmp2[{#}]}' ::: $(seq 10)


                    The resulting output with -v is similar to



                    my_importer; pf ${tmp1[2]} ${tmp2[2]}
                    my_importer; pf ${tmp1[1]} ${tmp2[1]}
                    my_importer; pf ${tmp1[5]} ${tmp2[5]}
                    my_importer; pf ${tmp1[3]} ${tmp2[3]}
                    job a=x1 b=y1
                    my_importer; pf ${tmp1[6]} ${tmp2[6]}
                    job a=x2 b=y2
                    my_importer; pf ${tmp1[7]} ${tmp2[7]}
                    job a=x4 b=y4
                    ...






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 30 '17 at 16:48









                    meuhmeuh

                    32.5k12256




                    32.5k12256








                    • 1





                      @meuth You should stop using my_importer and use env_parallel instead.

                      – Ole Tange
                      Sep 30 '17 at 18:45














                    • 1





                      @meuth You should stop using my_importer and use env_parallel instead.

                      – Ole Tange
                      Sep 30 '17 at 18:45








                    1




                    1





                    @meuth You should stop using my_importer and use env_parallel instead.

                    – Ole Tange
                    Sep 30 '17 at 18:45





                    @meuth You should stop using my_importer and use env_parallel instead.

                    – Ole Tange
                    Sep 30 '17 at 18:45


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395298%2fgnu-parallel-two-parameters-from-array-as-parameter%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                    Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                    Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...