Given your sample input/output then all you need is this, using GNU awk for gensub():
$ awk -F- {print (gensub(/.*\./,"",1,$1) < gensub(/.*\./,"",1,$2) ? $1 FS $2 : $2 FS $1)}' file
192.168.1.1-192.168.1.2
10.0.0.10-10.0.0.8
172.16.0.5-172.16.0.9
With other awks just use a couple of local vars and sub().
If, however, you need a solution that works when some other part of the IP addrs than just the final segment can be different on a given line (e.g. 172.16.0.5-172.15.0.9
), then this will work in any awk:
$ cat tst.awk
BEGIN { FS="-" }
{
split($1,t,/\./)
beg = sprintf("%03d%03d%03d%03d", t[1], t[2], t[3], t[4])
split($2,t,/\./)
end = sprintf("%03d%03d%03d%03d", t[1], t[2], t[3], t[4])
print (beg < end ? $1 FS $2 : $2 FS $1)
}
$ awk -f tst.awk file
192.168.1.1-192.168.1.2
10.0.0.8-10.0.0.10
172.16.0.5-172.16.0.9
$ echo '172.16.0.5-172.15.0.9' | awk -f tst.awk
172.15.0.9-172.16.0.5
If you're considering using a shell loop just to manipulate text then make sure you read and fully understand https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice first.