Solved

How to eliminate duplicates from a list of hosts in groups

Posted on 2016-09-16
2
58 Views
Last Modified: 2016-09-19
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
Comment
Question by:mickt
2 Comments
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 41801946
hosts=($(echo ${hosts[@]} | tr ' ' '\n' | sort -u))

Open in new window

0
 

Author Closing Comment

by:mickt
ID: 41804402
Cheers simon3270.

Tweaked so as to have single def.

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to dynamically set the form action using jQuery.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question