Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

RFC1795

IS-IT--Management
Feb 13, 2003
76
US
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!
 
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:
[COLOR=#0000ff]# run:[/color]
[COLOR=#0000ff]# awk -f rfc1795.awk rfc1795.txt[/color]
[COLOR=#6a5acd]BEGIN[/color] {
  [COLOR=#6a5acd]FS[/color] = [COLOR=#ff00ff]"[ ]*-[ ]*"[/color]
}

[COLOR=#6a5acd]$2[/color] == [COLOR=#ff00ff]""[/color] {
  [COLOR=#0000ff]# if only one address is given: print it[/color]
  [COLOR=#a52a2a][b]print[/b][/color] [COLOR=#6a5acd]$1[/color]
}

[COLOR=#6a5acd]$2[/color] != [COLOR=#ff00ff]""[/color] {
   [COLOR=#0000ff]# when range of IP addresses is given: [/color]
   [COLOR=#0000ff]# split beginning and ending IP adresses into arrays[/color]
   [COLOR=#008b8b]split[/color]([COLOR=#6a5acd]$1[/color][COLOR=#6a5acd],[/color] ip_from[COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"."[/color])
   [COLOR=#008b8b]split[/color]([COLOR=#6a5acd]$2[/color][COLOR=#6a5acd],[/color] ip_to[COLOR=#6a5acd],[/color] [COLOR=#ff00ff]"."[/color])
   [COLOR=#0000ff]# enumerate all IP adresses[/color]
   [COLOR=#a52a2a][b]for[/b][/color](i=ip_from[[COLOR=#6a5acd]4[/color]][COLOR=#6a5acd];[/color] i <= ip_to[[COLOR=#6a5acd]4[/color]][COLOR=#6a5acd];[/color] i++) {
     [COLOR=#a52a2a][b]printf[/b][/color]([COLOR=#ff00ff]"[/color][COLOR=#6a5acd]%d[/color][COLOR=#ff00ff].[/color][COLOR=#6a5acd]%d[/color][COLOR=#ff00ff].[/color][COLOR=#6a5acd]%d[/color][COLOR=#ff00ff].[/color][COLOR=#6a5acd]%d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd],[/color] ip_from[[COLOR=#6a5acd]1[/color]][COLOR=#6a5acd],[/color] ip_from[[COLOR=#6a5acd]2[/color]][COLOR=#6a5acd],[/color] ip_from[[COLOR=#6a5acd]3[/color]][COLOR=#6a5acd],[/color] 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.
 
@mikrom Thank you so much!! That is exactly what I was after.. tested and working 100% fine [bigsmile]

Brilliant solution.. and I love it! :)

[thanks2]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top