Damon Cortesi's blog

Musings of an entrepreneur.

Shell Script to Condense Hosts

| Comments

I came across a fairly large lists of hosts I had to paste into a document tonight, some of which were in contiguous ip ranges, but all of which were listed as single ip’s. For example, I had

192.168.1.1 192.168.1.8 192.168.10.3 192.168.1.9 192.168.1.10 192.168.10.2 192.168.1.2 192.168.10.1

and I wanted

192.168.1.1-2 192.168.1.8-10 192.168.10.1-3

See…it’s a little bit cleaner, isn’t it? So what better to do than put on some music to code by, grab a Hacker-Pschorr, and start doing a little shell scripting. An hour or two later, I whipped out this little sucker below. (in which I already found and corrected a bug while writing this post…) So, needless to say, you’re on your own with the code and I assume no responsibility. ;) Just make a file name hostlist with the list of ip addresses (doesn’t need to be sorted or even unique), and run the shell script and it will output the condensed list. Need I say how much I love automation. :)

Comments