0

My first post here, lets see how this goes.

Working on an issue where I have multiple "host lists". one set for linux, one for windows, one for printers.

I also have a list of what is currently being monitored.

I have been working on some code to try and unify the host lists and then get a diff of that list and the current monitoring list.

I found some good code I found on here:

Array1=( "key1" "key2" "key3" "key4" "key5" "key6" "key7" "key8" "key9" "key10" )
Array2=( "key1" "key2" "key3" "key4" "key5" "key6" ) 

Array3 =diff(Array1, Array2)

Array3 ideally should be : Array3=( "key7" "key8" "key9" "key10" )

This gives me the list of what needs to be added and works incredibly well.

Lets assume that Array2 contained an element whose value was "key11"

If I use the same diff I get all of the previous values AND key11.

Array1=( "key1" "key2" "key3" "key4" "key5" "key6" "key7" "key8" "key9" "key10" )
Array2=( "key1" "key2" "key3" "key4" "key5" "key6" "key11") 

Array3 =diff(Array1, Array2)

this outputs key 11 key10 .....

I guess I need to do two things:

  1. a list of all things that 1!=2 but NOT 2!=1

  2. a list of all things that 2!=1 but NOT 1!=2

I am hoping to account for the fact that these are being filled from a CSV whose content I do not control and the variation of naming conventions for each of the host lists means that the order/number/etc of the arrays will be unknown. How would I do that? I can see the logic for this just not the code. Any help is tremendously appreciated. Please save me from agonizing over this all weekend.

  • 1
    What is `Array3 =diff(Array1, Array2)` supposed to do? Because it's definitely not bash. Did you copy the code from the question [here](http://stackoverflow.com/questions/2312762/compare-difference-of-two-arrays-in-bash/2315541), instead of using one of the answers? – 123 May 12 '17 at 17:07
  • Is there a reason that you need bash? Seems like this might be easier to accomplish in a language that has support for `set`s. For example, in python you could just do `set(Array1) - set(Array2)` – 0x5453 May 12 '17 at 17:11
  • If you have each list of keys in separate files (or can make it so), this could be an easy job for the `comm(1)` command. Read the fine manual. – Jens May 12 '17 at 17:13
  • @Jens You could use process sub and print the array – 123 May 12 '17 at 17:20
  • 1
    If you need to process data (and not just run other programs), `bash` (or any shell language) is the wrong choice. – chepner May 12 '17 at 17:43
  • The array 3 diff, is calling the awk an awk command I found in the answer to a question here. unfortunately, due to the security of the environment we can not run any installed languages. trying to automate everything on this one machine to minimize the number of inputs/outputs. – Dave Wainwright May 15 '17 at 14:09

1 Answers1

0

You can get an array of the difference of two arrays like so:

Array3=($(sort <(printf "%s\n" "${Array1[@]}") <(printf "%s\n" "${Array2[@]}") | uniq -u))

Where we first sort the arrays by presenting them as temporary fds with the <() process substitution, in which the arrays are printed with each item on a new line.

That is then piped into uniq -u that only prints out unique items, and wrapping the whole thing first in $() captures the strings, which are then inputted into the wrapping array ().

Olli K
  • 1,552
  • 1
  • 14
  • 13
  • This is a little easier to follow and the explanation was great. But how would I get things that are in one list but not the other? – Dave Wainwright May 15 '17 at 11:23