×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Expanding IP ranges into single IP addresses (AWK or Other method best?)

Expanding IP ranges into single IP addresses (AWK or Other method best?)

Expanding IP ranges into single IP addresses (AWK or Other method best?)

(OP)
Hi All,

Which is the best method for me to handle this? I'm thinking awk might be the way to go perhaps, but not sure where to start even. I have a text file, which contains IP addresses and ranges as per this example:


10.10.115.69
10.10.128.6 - 10.10.128.7
10.10.128.20
10.10.128.28
10.10.128.38 - 10.10.128.53
10.10.128.70 - 10.10.128.71
10.10.128.130 - 10.10.128.144
10.10.128.232 - 10.10.128.233
10.10.130.5
10.10.132.5


I'm trying to get them all into their own individual address. So as per above on the line '10.10.128.38 - 10.10.128.53' .. I would want that to convert to:

Expected output:

10.10.128.20
10.10.128.28
10.10.128.38
10.10.128.39
10.10.128.40
<snip>
10.10.128.52
10.10.128.53
10.10.128.70
10.10.128.71
<etc.>

Keeping of course the single host IPs included into output, just expanding the range parts.

My expected output is as above, I'm not after a generator, rather a way to expand the ranges, the lines with '-' in them <start IP> - <end_IP> while keeping the single ones that are there still.

Hope that makes sense.

Thanks!

RE: Expanding IP ranges into single IP addresses (AWK or Other method best?)

Hi RFC1795,

IMO, with awk you have made a good choice. I tried it and it seems not to be very complicated.

First I created an input file as you suggested:
rfc1795.txt

CODE

10.10.115.69
10.10.128.6 - 10.10.128.7
10.10.128.20
10.10.128.28
10.10.128.38 - 10.10.128.53
10.10.128.70 - 10.10.128.71
10.10.128.130 - 10.10.128.144
10.10.128.232 - 10.10.128.233
10.10.130.5
10.10.132.5 

Then I wrote this script
rfc1795.awk

CODE

# run:
# awk -f rfc1795.awk rfc1795.txt
BEGIN {
  FS = "[ ]*-[ ]*"
}

$2 == "" {
  # if only one address is given: print it
  print $1
}

$2 != "" {
   # when range of IP addresses is given: 
   # split beginning and ending IP adresses into arrays
   split($1, ip_from, ".")
   split($2, ip_to, ".")
   # enumerate all IP adresses
   for(i=ip_from[4]; i <= ip_to[4]; i++) {
     printf("%d.%d.%d.%d\n", ip_from[1], ip_from[2], ip_from[3], i)          
   }
} 

which generates this output:

CODE

$ awk -f rfc1795.awk rfc1795.txt
10.10.115.69
10.10.128.6
10.10.128.7
10.10.128.20
10.10.128.28
10.10.128.38
10.10.128.39
10.10.128.40
10.10.128.41
10.10.128.42
10.10.128.43
10.10.128.44
10.10.128.45
10.10.128.46
10.10.128.47
10.10.128.48
10.10.128.49
10.10.128.50
10.10.128.51
10.10.128.52
10.10.128.53
10.10.128.70
10.10.128.71
10.10.128.130
10.10.128.131
10.10.128.132
10.10.128.133
10.10.128.134
10.10.128.135
10.10.128.136
10.10.128.137
10.10.128.138
10.10.128.139
10.10.128.140
10.10.128.141
10.10.128.142
10.10.128.143
10.10.128.144
10.10.128.232
10.10.128.233
10.10.130.5
10.10.132.5 
I thing, that this could be what you needed.

RE: Expanding IP ranges into single IP addresses (AWK or Other method best?)

(OP)
@mikrom Thank you so much!! That is exactly what I was after.. tested and working 100% fine bigsmile

Brilliant solution.. and I love it! smile

thanks2

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close