简体   繁体   English

字符串的倒序

[英]Reverse order of a string

I want to "reverse" the order of the four octets (bytes) that make up an ip address. 我想“逆向”组成一个IP地址的四个八位字节(字节)的顺序。

Suppose I have this ip: 假设我有这个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. 然后最终删除反向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: 您也可以使用bash数组来做到这一点:

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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