Shell script to convert IP range csv into a list of single IP's

Hi All,

I am looking for some help to convert a csv with IP ranges in.. in the format e.g.

1.1.1.2, 1.1.1.5
2.1.1.10, 2.1.1.20

and would be looking to output as follows:

1.1.1.2
1.1.1.3
1.1.1.4
1.1.1.5
2.1.1.10
2.1.1.11 

etc etc up to 2.1.1.20

I have tried a few google things out but so far no luck... i have never done any shell scripting before...

Thanks

Hello zippyzip,

Please use code tags for commands/codes/Inputs you are using into your posts as per forum rules. Could you please try following and let me know if this helps.

awk -F", " '{m=split($1, A,".");n=split($2, B,".");num=A[m];till_num=B[n];Q=A[1]"."A[2]"."A[3];for(;num<=till_num;num++){print Q OFS num}}'  Input_file

Output will be as follows.

1.1.1 2
1.1.1 3
1.1.1 4
1.1.1 5
2.1.1 10
2.1.1 11
2.1.1 12
2.1.1 13
2.1.1 14
2.1.1 15
2.1.1 16
2.1.1 17
2.1.1 18
2.1.1 19
2.1.1 20
 

Thanks,
R. Singh

1 Like

Thanks Ravinder gives me something to work with...

Another awk:

awk '{for(i=$4; i<=$8; i++) print $1,$2,$3,i}' FS='[.]|, *' OFS=. file
1.1.1.2
1.1.1.3
1.1.1.4
1.1.1.5
2.1.1.10
2.1.1.11
2.1.1.12
2.1.1.13
2.1.1.14
2.1.1.15
2.1.1.16
2.1.1.17
2.1.1.18
2.1.1.19
2.1.1.20

Given your IP range spans byte boundaries (like 10.1.1.251, 10.1.2.5 ), this might be of some interest:

while IFS=", " read IP1 IP2
  do    BIP1=$((0x$(printf "%02X" ${IP1//./ })));
        BIP2=$((0x$(printf "%02X" ${IP2//./ })));
        for ((I=BIP1; I<=BIP2; I++))
          do printf "%d.%d.%d.%d\n" $((I>>24)) $((I>>16&255)) $((I>>8&255)) $((I&255))
          done
  done < file
10.1.1.251
10.1.1.252
10.1.1.253
10.1.1.254
10.1.1.255
10.1.2.0
10.1.2.1
10.1.2.2
10.1.2.3
10.1.2.4
10.1.2.5
1 Like