Solved

How to eliminate duplicates from a list of hosts in groups

Posted on 2016-09-16
2
50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to convert a simple VB script to PowerShell 3 66
Combine DSQuery result into loop script 6 52
Recursive folder search vbscript 7 40
Disk Ram Peak 1 33
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to count occurrences of each item in an array.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now