Grouping hostnames for pdsh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grouping hostnames for pdsh
# 1  
Old 07-06-2016
Grouping hostnames for pdsh

Hi All,

I have a list of hostnames in a text file and I would like to craft a bash script that would group them for use with pdsh.

For example... I would like to group the following from a file:

Code:
hostname1000
hostname1001
hostname1002
hostname1003
hostname1004
hostname2000
hostname2001
hostname2002

into hostname[1000-1004,2000-2002]

I tried some prelimenary scripts like:

Code:
num=1;for a in $(cat range_hostname.txt |cut -c 1-12 |sort) ; do echo ${a}${num}; num=$[num+1 ];done

to get hostname10, for example, and put a count after, but this just counts all the lines and puts the count at the end. Not like a group of like names like I need.

Any ideas for this?

Thanks!
Joe



Moderator's Comments:
Mod Comment Please use code tags as required by forum rules!

Last edited by RudiC; 07-06-2016 at 11:27 AM.. Reason: Added (i)code tags.
# 2  
Old 07-06-2016
the shell might not be the fittest tool. Try awk:
Code:
awk '
                {HN = $1
                 sub (/[0-9]*$/, _, HN)
                 sub (/^[a-z]*/, _, $1)
                }
NR == 1         {printf "%s[%d-", HN, $1
                }
NR > 1 &&
$1 != LAST+1    {printf "%d,%d-", LAST, $1
                }
                {LAST = $1
                }
END             {printf "%d]\n", $1
                }
' file
hostname[1000-1004,2000-2002]

This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-06-2016
Thats great, RudiC!

Now I need to do some reading so I can figure out how it works :-D.

Thanks so much!
Joe

*Also, I will use code tags in future posts!

---------- Post updated at 10:02 AM ---------- Previous update was at 10:01 AM ----------

Oh, one more quick question on this...

How can I add number padding?

For example, if the hostname is

Code:
hostname0100
hostname0101
hostname0102

I would need to account for the zero before the '1'.

Thanks!
Joe
# 4  
Old 07-06-2016
Code:
awk '
                {HN = $1
                 sub (/[0-9]*$/, _, HN)
                 sub (/^[a-z]*/, _, $1)
                }
NR == 1         {printf "%s[%04d-", HN, $1
                }
NR > 1 &&
$1+0 != LAST+1    {printf "%04d,%04d-", LAST, $1
                }
                {LAST = $1
                }
END             {printf "%04d]\n", $1
                }
' file
hostname[1000-1004,2000-2002,0100-0102]

This User Gave Thanks to RudiC For This Post:
# 5  
Old 07-06-2016
That did the trick!

Thanks a million!
Joe
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check for valid hostnames

Hello, I am trying to develop a script to check for valid hostnames. Below are the prerequisites for a valid hostname which I got from wiki : Hostnames are composed of series of labels concatenated with dots, as are all domain names. For example, "en.wikipedia.org" is a hostname. Each label... (8 Replies)
Discussion started by: rahul2662
8 Replies

2. Shell Programming and Scripting

Ip address for multiple hostnames

i have a file which has 100 servers,i want a script which can output me ip address and hostname for that server. Thanks in advance!! input file abc.com output file should be abc.com 192.168.1..1 (1 Reply)
Discussion started by: Moon1234
1 Replies

3. UNIX for Dummies Questions & Answers

Cyrillic Hostnames?

As the title suggests, I'm wondering if its possible to set a hostname to a name or word with Cyrillic letters (e.g. - Like the Russian alphabet). I tried installing the cyrillic-console package in Debian. I switched my desktop to Russian as well. However, when I try to set the hostname I get that... (2 Replies)
Discussion started by: Azrael
2 Replies

4. Programming

Getting All VM Hostnames Using Pysphere

I need to obtain a list of all the hostname of all the VM's that reside on two ESXI servers using pysphere. I have attempted using the basic methods but an unable to get a hostname in my testing using: from pysphere import VIServer server = VIServer() ... (0 Replies)
Discussion started by: metallica1973
0 Replies

5. UNIX for Dummies Questions & Answers

Is there a unix command to find ALL hostnames for an ip address?

I am trying to determine if there are several url/host names for an IP address. Is there a UNIX command to find ALL host names for an IP address? Thank you in advance. (3 Replies)
Discussion started by: rukasu
3 Replies

6. Shell Programming and Scripting

Compare file of hostnames with table in MySQL DB

Hi all, i have a list of files that contains some PC hostname, then i need to enumerate every hostname and check if there's a table with same name of the hosts in MySQL database XYZ. I need this because have to decide automatically if i have to make a create table or a insert into an existent... (2 Replies)
Discussion started by: maxlamax
2 Replies

7. Shell Programming and Scripting

regex help to get unique hostnames

Hi I have a file containing hostnames like this (host=myhost.domain.com) or (host=myhost) i need to extarct the unique hostnames without the domain names from that file. so my output should be myhost (without domain names) But my regex skills are rusty i tried grep "host"... (4 Replies)
Discussion started by: xiamin
4 Replies

8. Shell Programming and Scripting

Scripting nslookup to resolve multiple hostnames

Hi Friends, I have a list of servers with their production names in a file. I want to know the best way eiter a command or a script that can do the following :- Append "-bkp" to each hostnames at the end And run nslookup and make sure I have valid backup IP add assigned to it. Any... (1 Reply)
Discussion started by: new2prog
1 Replies

9. IP Networking

DHCP and Hostnames

I have a linksys DSL router. I run a mixed environment of Windows, Linux and Solaris. The routers dhcp tables show the windows machines hostnames and associated IP's ok but the unix bases mahines don't have associated hostnames in the DHCP tables. I can ping by IP but not my the machines... (5 Replies)
Discussion started by: mr16ga
5 Replies

10. UNIX for Dummies Questions & Answers

newbie question about hostnames

When I log into bash it will say my username @localhost I wondering how do I change localhost to another such as draco.I've the using the hostname command as root but it changes back to localhost after I reboot.I was wondering how would I hide my ISP hostname in linux.Because when I log into an IRC... (1 Reply)
Discussion started by: angelfly
1 Replies
Login or Register to Ask a Question