I would like to edit my hosts file to match my current IPs, I could do it in Python or AWK, but I was wondering if there is a way in VIM...
let's see what I mean, my hosts looks like that:
192.168.11.172 blazer blazer.mydomain
192.168.11.173 faster faster.mydomain
...
192.168.11.225 schurtig schurtig.mydomain
Now, I want to increment every IP by 32 so the end result would be:
192.168.11.202 blazer blazer.mydomain
192.168.11.203 faster faster.mydomain
...
192.168.11.257 schurtig schurtig.mydomain
If I put my cursor on the right IP, and type
30 and CTRL+A
it does what I want.
The only problem, I have about 300 hosts ... and I need to do it once every 3 months ... Although I could do it in Python, seeing the file in vim feels safer, because I don't need to run a script and then control the result after that.
The following changes only the first line:
let i=172 | g/172/s//\=i+30/
I want to repeat this in a loop for different IP ranges being able to say how many times i should be incremented, is there a "trick" that does that?
Thanks in advance for the efforts, Oz
Try this:
:let i=30 | %s/^\(\d*\.\d*\.\d*\.\)\(\d*\)/\=submatch(1).(submatch(2)+i)
\(\d*\.\d*\.\d*\.\)
(retrieved by submatch(1)
) matches the IP first three numbers and dots (eg 192.168.11.
) and \(\d*\)
(retrieved by submatch(2)
) matches the IP last number.
I think this is a better way to control it:
:let i=30 | %s/^\(\d*\.\d*\.\d*\.\)\(\d*\)/\=submatch(1).(submatch(2)+i)/gc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With