[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 91
  • Last Modified:

How to eliminate duplicates from a list of hosts in groups

I have groups containing hosts as follows.

    groupA=(server01 server02 server03)
    groupB=(server01 server02 server03)
    groupC=(server01 server02 server03)
    groupD=(server04 server05)

List of hosts as follows.

    hosts=(${groupA[@]} ${groupB[@]} ${groupC[@]} ${groupD[@]})

I do something as follows.

    for host in ${hosts[@]}; do echo $host; done
    server01
    server02
    server03
    server01
    server02
    server03
    server01
    server02
    server03
    server04
    server05

While this works, there is duplication, which slows things.  How do I include servers only once?
0
mickt
Asked:
mickt
1 Solution
 
simon3270Commented:
hosts=($(echo ${hosts[@]} | tr ' ' '\n' | sort -u))

Open in new window

0
 
micktAuthor Commented:
Cheers simon3270.

Tweaked so as to have single def.

hosts=($(echo ${groupA[@]} ${groupB[@]} ${groupC[@]} ${groupD[@]} | tr ' ' '\n' | sort -u))
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now