Solved

How to eliminate duplicates from a list of hosts in groups

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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 …

746 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

12 Experts available now in Live!

Get 1:1 Help Now