1

I want to compare the below arrays and print/store only the dissimilar items in another array. can you pls help.

eg Array1  for :
20150313 20150324 20150325 20150326 20150330 20150331 20150401 20150402 20150403 20150406 20150407 20150408 20150409 20150410 20150413 20150414 20150415 20150416 20150417 20150418 20150420 20150421 20150422 20150423 20150424 20150427 20150428

eg Array 2 for :
20150313 20150323 20150324 20150325 20150326 20150327 20150330 20150331 20150401 20150402 20150403 20150406 20150407 20150408 20150409 20150410 20150413 20150414 20150415 20150416 20150417 20150418 20150420 20150421 20150422 20150423 20150424 20150427 20150428 20150313 20150323 20150324 20150325 20150326 20150327 20150330 20150331 20150401 20150402 20150403 20150406 20150407 20150408 20150409 20150410 20150413 20150414 20150415 20150416 20150417 20150418 20150420 20150421 20150422 20150423 20150424 20150427 20150428

if i try this in bash, it only prints the values in array 1

Array3=()
$ for i in "${Array1[@]}"; do
>     skip=
>     for j in "${Array2[@]}"; do
>         [[ $i == $j ]] && { skip=1; break; }
>     done
>     [[ -n $skip ]] || Array3+=("$i")
> done

pls assist.

3
  • If I understand correctly, you're not trying to compare a1[i] to a2[i], cause offsets are possible. An easy way I can think of would be to put them in textual variables with values separated by newlines, and use diff to compare them. However, if the order doesn't matter and only the existence of elements matter, i would sort - uniq first. Commented May 14, 2015 at 10:27
  • i dont want to do sort -uniq because it gives only unique in both the arrays. i want to print noly the elements in array1 which are not in array 2 Commented May 14, 2015 at 10:30
  • 1
    What output are you expecting, post it. Commented May 14, 2015 at 10:34

1 Answer 1

1

To get set2 - set1 you can use grep with process substitution:

grep -wFvf <(printf "%s\n" "${arr1[@]}") <(printf "%s\n" "${arr2[@]}")
20150323
20150327
20150323
20150327

And to store results in another array:

arr3=($(grep -wFvf <(printf "%s\n" "${arr1[@]}") <(printf "%s\n" "${arr2[@]}")))

PS: To get set1 - set2 use:

grep -wFvf <(printf "%s\n" "${arr2[@]}") <(printf "%s\n" "${arr1[@]}")
Sign up to request clarification or add additional context in comments.

8 Comments

it says below error in bash: command substitution:syntax error near unexpected token (' command substitution:grep -wFvf <(printf "%s\n" "${dates[@]}") <(printf "%s\n" "${volcker_date[@]}"))'
arr1=20150323 20150327 20150328 20150329 arr2=20150323 20150324 i want to print 20150324 is not available in arr2 and 20150327 20150328 20150329 are not available in arr1. sorry if i was not clear in first thread
Are you sure you're using BASH? Run bash, enter into bash shell and then try above commands.
yes in bash, it now printing the below 20150323: command not found Not all the desimilar dates
grep -wFvf <(printf "%s\n" "${arr2[@]}") <(printf "%s\n" "${arr1[@]}") is working. many thanks. i got another issue there. let me check it. thanks all guys!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.