Solved

How to eliminate duplicates from a list of hosts in groups

Posted on 2016-09-16
2
73 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 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