Reputation: 6739
I want to "reverse" the order of the four octets (bytes) that make up an ip address.
Suppose I have this ip:
202.168.56.32
I need to convert into:
32.56.168.202
and then ultimately remove the first octet in the reversed ip. Final result:
56.168.202
My attempts:
echo 202.168.56.32 | rev
But it's returning :
23.65.861.202
Upvotes: 1
Views: 351
Reputation: 11069
Or you could use sed.
echo 202.168.56.32 | sed -e 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\4.\3.\2.\1/g'
Upvotes: 0
Reputation: 206851
This should do the trick:
echo 202.168.56.32|awk -F. '{print $3"."$2"."$1}'
You could also do it with bash arrays:
ip=202.168.56.32
parts=(${ip//./ })
echo ${parts[2]}.${parts[1]}.${parts[0]}
Upvotes: 9