简体   繁体   中英

Reverse order of a string

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

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]}

或者您可以使用sed。

echo 202.168.56.32 | sed -e 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\4.\3.\2.\1/g'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM