Ip address sorting

Hello folks,

I am sorting ip address file i have file having ips like

192.168.1.1
192.168.2.2
192.168.1.3
192.168.1.2
192.168.11.1
192.168.1.4
192.168.2.1

I have tried below code

i=1
while :
do
echo $i
x=$(cat file | grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.$i\{1,3\}\.[0-9]\{1,3\}')
echo $x
let i++
done

expected output, i want to sort ips in such a way first it will complete 192.168.1.1 to 192.168.1.254 then search 192.168.2.1 to 192.168.2.254 and so on go to 192.168.33.1 to 192.168.33.254.

192.168.1.1
192.168.1.2
192.168.1.3
192.168.2.1
192.168.2.2
192.168.1.4
192.168.11.1

Try this:

sort -t . -k 3,3n -k 4,4n file

This is not working, showing


192.168.30.
192.168.15.
192.168.30.
192.168.37.
.......

192.168.6.
192.168.30.
192.168.37.

What is your input data and which OS are you running? What you posted doesn't correspond with the input you posted earlier. The sort command doesn't change the data, only the order in which it's shown and your IP's seem to be missing some numbers.

I am using CENTOS Linux, There are hundered of ips, i just take few ips from order, but order is not correct as mentioned, if ips are not mentioned then no issue it will search for different subnet and make it order as mentioned above, if not present then discard that let it print the ips in order.

Did you try the sort command directly on your input file instead of implementing it in your while loop?

$ cat file
192.168.1.1
192.168.2.2
192.168.1.3
192.168.1.2
192.168.11.1
192.168.1.4
192.168.2.1
$ sort -t . -k 3,3n -k 4,4n file
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.2.1
192.168.2.2
192.168.11.1

Yes i have tried directly without loop, result is not correct. One more thing hostname is also mention in the file. File is like that.

hostname  ip-address
hostname  ip-address
hostname  ip-address
hostname  ip-address
hostname  ip-address

sort -t "." -k3,4 -n FileName

@learnbash
You have nearly 300 post here, you should now better than posting example that is not like your data, and expect us to solve it.
All the post above assume that IP is in column 1 and not 2 .
If you had posted some real data this would have been solved quick.

cat file
www.site.no 192.168.1.1
www.site1.no 192.168.2.2
www.site2.no 192.168.1.3
www.site3.no 192.168.1.2
www.site4.no 192.168.11.1
www.site5.no 192.168.1.4
www.site6.no 192.168.2.1
awk '{print $2,$1}' file | sort -t . -k3,4 -n | awk '{print $2,$1}'
www.site.no 192.168.1.1
www.site3.no 192.168.1.2
www.site2.no 192.168.1.3
www.site5.no 192.168.1.4
www.site6.no 192.168.2.1
www.site1.no 192.168.2.2
www.site4.no 192.168.11.1