Find overlap/intersection of lists with bash
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
add a comment |
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
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
add a comment |
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
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
bash shell-script git array bash-array
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
How about
for FN in ${first_list[@]}; do [[ ${second_list[@]} =~ $FN ]] && third_list+=($FN); done
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
How about
for FN in ${first_list[@]}; do [[ ${second_list[@]} =~ $FN ]] && third_list+=($FN); done
add a comment |
How about
for FN in ${first_list[@]}; do [[ ${second_list[@]} =~ $FN ]] && third_list+=($FN); done
add a comment |
How about
for FN in ${first_list[@]}; do [[ ${second_list[@]} =~ $FN ]] && third_list+=($FN); done
How about
for FN in ${first_list[@]}; do [[ ${second_list[@]} =~ $FN ]] && third_list+=($FN); done
answered Aug 17 '18 at 22:28
RudiC
4,2041312
4,2041312
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Dec 20 '18 at 18:20
answered Dec 20 '18 at 18:08
Kusalananda
122k16229374
122k16229374
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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