Find overlap/intersection of lists with bash












0














Say I have these two "lists":



#!/usr/bin/env bash

git fetch origin;

first_list=( );
second_list=( );

git branch --merged "remotes/origin/dev" | tr -d ' *' | while read branch; do
first_list+=( "$branch" );
done


git branch --merged HEAD | tr -d ' *' | while read branch; do
second_list+=( "$branch" );
done


I need to create a third list that holds the intersection of elements in the first and second lists. How can I do that?










share|improve this question
























  • better than what?
    – RudiC
    Aug 17 '18 at 22:13










  • better than zero which is what I have right now. I changed the verbiage, I am just looking for a decent solution to this.
    – Alexander Mills
    Aug 17 '18 at 22:21












  • Potentially useful: unix.stackexchange.com/q/11343/117549 — includes a zsh array option
    – Jeff Schaller
    Aug 18 '18 at 0:22
















0














Say I have these two "lists":



#!/usr/bin/env bash

git fetch origin;

first_list=( );
second_list=( );

git branch --merged "remotes/origin/dev" | tr -d ' *' | while read branch; do
first_list+=( "$branch" );
done


git branch --merged HEAD | tr -d ' *' | while read branch; do
second_list+=( "$branch" );
done


I need to create a third list that holds the intersection of elements in the first and second lists. How can I do that?










share|improve this question
























  • better than what?
    – RudiC
    Aug 17 '18 at 22:13










  • better than zero which is what I have right now. I changed the verbiage, I am just looking for a decent solution to this.
    – Alexander Mills
    Aug 17 '18 at 22:21












  • Potentially useful: unix.stackexchange.com/q/11343/117549 — includes a zsh array option
    – Jeff Schaller
    Aug 18 '18 at 0:22














0












0








0







Say I have these two "lists":



#!/usr/bin/env bash

git fetch origin;

first_list=( );
second_list=( );

git branch --merged "remotes/origin/dev" | tr -d ' *' | while read branch; do
first_list+=( "$branch" );
done


git branch --merged HEAD | tr -d ' *' | while read branch; do
second_list+=( "$branch" );
done


I need to create a third list that holds the intersection of elements in the first and second lists. How can I do that?










share|improve this question















Say I have these two "lists":



#!/usr/bin/env bash

git fetch origin;

first_list=( );
second_list=( );

git branch --merged "remotes/origin/dev" | tr -d ' *' | while read branch; do
first_list+=( "$branch" );
done


git branch --merged HEAD | tr -d ' *' | while read branch; do
second_list+=( "$branch" );
done


I need to create a third list that holds the intersection of elements in the first and second lists. How can I do that?







bash shell-script git array bash-array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 17 '18 at 22:21

























asked Aug 17 '18 at 21:13









Alexander Mills

2,15911442




2,15911442












  • better than what?
    – RudiC
    Aug 17 '18 at 22:13










  • better than zero which is what I have right now. I changed the verbiage, I am just looking for a decent solution to this.
    – Alexander Mills
    Aug 17 '18 at 22:21












  • Potentially useful: unix.stackexchange.com/q/11343/117549 — includes a zsh array option
    – Jeff Schaller
    Aug 18 '18 at 0:22


















  • better than what?
    – RudiC
    Aug 17 '18 at 22:13










  • better than zero which is what I have right now. I changed the verbiage, I am just looking for a decent solution to this.
    – Alexander Mills
    Aug 17 '18 at 22:21












  • Potentially useful: unix.stackexchange.com/q/11343/117549 — includes a zsh array option
    – Jeff Schaller
    Aug 18 '18 at 0:22
















better than what?
– RudiC
Aug 17 '18 at 22:13




better than what?
– RudiC
Aug 17 '18 at 22:13












better than zero which is what I have right now. I changed the verbiage, I am just looking for a decent solution to this.
– Alexander Mills
Aug 17 '18 at 22:21






better than zero which is what I have right now. I changed the verbiage, I am just looking for a decent solution to this.
– Alexander Mills
Aug 17 '18 at 22:21














Potentially useful: unix.stackexchange.com/q/11343/117549 — includes a zsh array option
– Jeff Schaller
Aug 18 '18 at 0:22




Potentially useful: unix.stackexchange.com/q/11343/117549 — includes a zsh array option
– Jeff Schaller
Aug 18 '18 at 0:22










2 Answers
2






active

oldest

votes


















1














How about



for FN in ${first_list[@]}; do [[ ${second_list[@]} =~ $FN ]] && third_list+=($FN); done





share|improve this answer





























    1














    Using an associative array as a helper to keep track of the elements in one list (as keys), and then quickly checking the elements of the other list against these:



    #!/bin/bash

    list1=( 1 3 5 6 7 8 bumble bee )
    list2=( 2 4 4 4 6 7 8 bee beer )

    declare -A seen

    for item in "${list1[@]}"; do
    seen[$item]=1
    done

    for item in "${list2[@]}"; do
    if [ -n "${seen[$item]}" ]; then
    intersection+=( "$item" )
    fi
    done

    echo 'Intersection:'
    printf 't%sn' "${intersection[@]}"


    This uses exact string matches to compare the elements between the two lists.



    Result:



    Intersection:
    6
    7
    8
    bee





    share|improve this answer























      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%2f463257%2ffind-overlap-intersection-of-lists-with-bash%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









      1














      How about



      for FN in ${first_list[@]}; do [[ ${second_list[@]} =~ $FN ]] && third_list+=($FN); done





      share|improve this answer


























        1














        How about



        for FN in ${first_list[@]}; do [[ ${second_list[@]} =~ $FN ]] && third_list+=($FN); done





        share|improve this answer
























          1












          1








          1






          How about



          for FN in ${first_list[@]}; do [[ ${second_list[@]} =~ $FN ]] && third_list+=($FN); done





          share|improve this answer












          How about



          for FN in ${first_list[@]}; do [[ ${second_list[@]} =~ $FN ]] && third_list+=($FN); done






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 17 '18 at 22:28









          RudiC

          4,2041312




          4,2041312

























              1














              Using an associative array as a helper to keep track of the elements in one list (as keys), and then quickly checking the elements of the other list against these:



              #!/bin/bash

              list1=( 1 3 5 6 7 8 bumble bee )
              list2=( 2 4 4 4 6 7 8 bee beer )

              declare -A seen

              for item in "${list1[@]}"; do
              seen[$item]=1
              done

              for item in "${list2[@]}"; do
              if [ -n "${seen[$item]}" ]; then
              intersection+=( "$item" )
              fi
              done

              echo 'Intersection:'
              printf 't%sn' "${intersection[@]}"


              This uses exact string matches to compare the elements between the two lists.



              Result:



              Intersection:
              6
              7
              8
              bee





              share|improve this answer




























                1














                Using an associative array as a helper to keep track of the elements in one list (as keys), and then quickly checking the elements of the other list against these:



                #!/bin/bash

                list1=( 1 3 5 6 7 8 bumble bee )
                list2=( 2 4 4 4 6 7 8 bee beer )

                declare -A seen

                for item in "${list1[@]}"; do
                seen[$item]=1
                done

                for item in "${list2[@]}"; do
                if [ -n "${seen[$item]}" ]; then
                intersection+=( "$item" )
                fi
                done

                echo 'Intersection:'
                printf 't%sn' "${intersection[@]}"


                This uses exact string matches to compare the elements between the two lists.



                Result:



                Intersection:
                6
                7
                8
                bee





                share|improve this answer


























                  1












                  1








                  1






                  Using an associative array as a helper to keep track of the elements in one list (as keys), and then quickly checking the elements of the other list against these:



                  #!/bin/bash

                  list1=( 1 3 5 6 7 8 bumble bee )
                  list2=( 2 4 4 4 6 7 8 bee beer )

                  declare -A seen

                  for item in "${list1[@]}"; do
                  seen[$item]=1
                  done

                  for item in "${list2[@]}"; do
                  if [ -n "${seen[$item]}" ]; then
                  intersection+=( "$item" )
                  fi
                  done

                  echo 'Intersection:'
                  printf 't%sn' "${intersection[@]}"


                  This uses exact string matches to compare the elements between the two lists.



                  Result:



                  Intersection:
                  6
                  7
                  8
                  bee





                  share|improve this answer














                  Using an associative array as a helper to keep track of the elements in one list (as keys), and then quickly checking the elements of the other list against these:



                  #!/bin/bash

                  list1=( 1 3 5 6 7 8 bumble bee )
                  list2=( 2 4 4 4 6 7 8 bee beer )

                  declare -A seen

                  for item in "${list1[@]}"; do
                  seen[$item]=1
                  done

                  for item in "${list2[@]}"; do
                  if [ -n "${seen[$item]}" ]; then
                  intersection+=( "$item" )
                  fi
                  done

                  echo 'Intersection:'
                  printf 't%sn' "${intersection[@]}"


                  This uses exact string matches to compare the elements between the two lists.



                  Result:



                  Intersection:
                  6
                  7
                  8
                  bee






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 20 '18 at 18:20

























                  answered Dec 20 '18 at 18:08









                  Kusalananda

                  122k16229374




                  122k16229374






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f463257%2ffind-overlap-intersection-of-lists-with-bash%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

                      Morgemoulin

                      Scott Moir

                      Souastre