awk getline 8 times and if $3 = 8 when subtracted from 1st line,print

I have kind of a strange one here. I have a file of consecutive /24 ip blocks. If there are 8 consecutive ip blocks which represent a /20 then I need to print the first line. I played around and did not get the results I need, especially when considering that the highest $3 will be is 255 and then start over to 0.

Here is a sample file

10.71.192.0
10.71.193.0
10.71.194.0
10.71.195.0
10.71.196.0
10.71.197.0
10.71.198.0
10.71.199.0
10.78.176.0
10.78.177.0
10.78.178.0
10.78.179.0
10.90.25.0
10.90.26.0
10.122.232.0
10.122.233.0
10.122.234.0
10.122.235.0
10.122.236.0
10.122.237.0
10.122.238.0
10.122.239.0
10.144.251.0
10.144.252.0
10.144.253.0
10.144.254.0
10.144.255.0
10.145.0.0
10.145.1.0
10.145.2.0

What I need to see output is:

10.71.192.0
10.122.232.0
10.144.251.0

I tried something like this but did not get good results

nawk -F"." '{p=$0; v=$3; getline;getline;getline;getline;getline;getline;getline;getline; d=$3 - v} d = 8 {print p}' myfile

First things first: 8 consecutive /24 blocks do not represent a /20 block but a /21 block and only if the lowest third number of the consecutive /24 ranges is divisible by 8.

Typo, sorry that is correct a /21.

Your sample output is not correct!

Try

awk  '{if (++C[$1$2($3-$3%8)] == 8) print $1,$2,($3-$3%8),$4}' FS="." OFS="." file
10.71.192.0
10.122.232.0
3 Likes

Nice solution RudiC !

I think it would be better to separate the fields in the index to avoid overflow between ranges.
I made an adaptation to your solution so that the CIDR prefix becomes variable:

awk -F. -v cp=21 'BEGIN{s=2^(24-cp)}{r=$1 FS $2 FS $3-$3%s FS $4} ++C[r]==s{print r}' file  
2 Likes

Above was a "sketch", a "draft", and I was thinking about making it a bit more flexible myself. Thank for your proposals; separating the index fields is a good one, as is the variable bit depth for the netmask... Although the requestor asked for 8; we just infer he wants to see if that subnet is "full".

Thank you both, worked very well.