在不同长度的字符串中的特定位置交换字符 [英] Swap characters in specific positions in strings of varying lengths

查看:100
本文介绍了在不同长度的字符串中的特定位置交换字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试学习sed,我在这里找到的示例是从05082012到20120805的日期交换,而我很难适应他们的当前需求。

I've been trying to learn sed and the examples I've found here are for swapping dates from 05082012 to 20120805 and I'm having trouble adapting them to my current need.

我需要将nsupdate脚本的IP地址10.4.13.22转换为22.13.4.10的反向查找。我最大的问题是有时每个八位位组可以更改长度,例如10.4.13.2和10.19.8.126

I need to convert an IP address 10.4.13.22 to a reverse lookup of 22.13.4.10 for a nsupdate script. My biggest problem is the fact that sometimes each octet can change lengths e.g. 10.4.13.2 and 10.19.8.126

感谢您的帮助!

echo 10.0.2.99 | sed 's/\(....\)\(....\)/\2\1/'

这是我目前尝试过的,只是基于此处的另一个问题,但是由于示例并未对....的含义提供太多解释,因此,我遇到了麻烦

this is currently what I've tried, just based off another question here, but since the examples don't provide much explanation as to what .... means, Im having trouble understanding what it does.

这是该命令的输出.2.910.09,我期望99.2.0.10

This is the output of that command .2.910.09 and I am expecting 99.2.0.10

直接,我想重新排列每个由。分隔的部分。

Directly, I want to rearrange each "section" that is separated by a "."

推荐答案

bruteforce

A "bruteforce" method to "reverse" an IPv4 address would be:

sed 's/\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)/\4.\3.\2.\1/g'

,或者对于GNU sed,

or, for GNU sed,

sed -r 's/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/\4.\3.\2.\1/g'

这篇关于在不同长度的字符串中的特定位置交换字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆