sort doesn't work on ip addresses too well, so here goes:
See how the following sorted output has 1, then 10, then 100, 110, then 3, then 5. This is all out of order.
user@user:~$ LISTING=`for X in 10.1 10.3 10.5 10.10 10.110 10.100; do echo 10.10.$X; done`
user@user:~$ echo -e "$LISTING" | sort
10.10.10.1
10.10.10.10
10.10.10.100
10.10.10.110
10.10.10.3
10.10.10.5
user@user:~$
The following will create an order structure which will work with the above sorted tree.
RANGE="[0-9] [0-9][0-9] [0-9][0-9][0-9]"; for ONE in $RANGE; do for TWO in $RANGE; do for THREE in $RANGE; do for FOUR in $RANGE; do echo $ONE\\\\.$TWO\\\\.$THREE\\\\.$FOUR; done; done; done; done | sed -e 's/$/\\$/' -e 's/^/\\^/' > ipgrep.txt
cat ipgrep.txt | while read; do echo "$LISTING" | grep $REPLY; done
You could also do both lines together like so:
RANGE="[0-9] [0-9][0-9] [0-9][0-9][0-9]"; for ONE in $RANGE; do for TWO in $RANGE; do for THREE in $RANGE; do for FOUR in $RANGE; do echo $ONE\\\\.$TWO\\\\.$THREE\\\\.$FOUR; done; done; done; done | sed -e 's/$/\\$/' -e 's/^/\\^/' | while read; do echo "$LISTING" | sort | grep $REPLY; done