LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Reformatting IP address (https://www.linuxquestions.org/questions/linux-networking-3/reformatting-ip-address-665317/)

Geneset 08-25-2008 02:34 PM

Reformatting IP address
 
Hi guys, me again,

I'll make this short

Whos magic with regex's and sed?

I am getting heading 0's from the ip routing table on my router, eg

0xx.00x.xxx.00x

I'm imagining something with sed wud be perfect but i cant get the right expression to match

I'm thinking s/\.0*/./ but it isnt working.


Ideas? Cheers

TB0ne 08-25-2008 02:50 PM

Quote:

Originally Posted by Geneset (Post 3259353)
Hi guys, me again,

I'll make this short

Whos magic with regex's and sed?
I am getting heading 0's from the ip routing table on my router, eg
0xx.00x.xxx.00x

I'm imagining something with sed wud be perfect but i cant get the right expression to match
I'm thinking s/\.0*/./ but it isnt working.
Ideas? Cheers

How about more information?? How do you want it formatted? What are you trying to do?

Geneset 08-25-2008 02:59 PM

get rid of the 0's as they are not recognised by any other tools, eg.

if an ipaddress being accessed is 10.1.1.1, i would read from this source 010.001.001.001, and i want to turn that back into 10.1.1.1

make more sense now?

I want to remove any zeros that immediatly follow a decimal or are at the very beginning of the line.

rocket357 08-25-2008 03:50 PM

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

Ugly as sin, but it works...heh

This would be insanely easy with a Python script, by the way...

Geneset 08-25-2008 05:05 PM

I could live with python...

Go on....

rocket357 08-25-2008 05:49 PM

Code:

#!/usr/bin/env python

def fix_ip(ip):
    octets = ip.split('.')
    for octet in octets:
        octet = octet.lstrip('0')
    return '.'.join(octets)

You'd have to write code to utilize this function, but at a bare minimum that's what it'd take.

Geneset 08-25-2008 05:56 PM

Simple, Elegant, I like it *steals*

Thanks for all the help guys :D

Kenhelm 08-26-2008 12:53 AM

This uses GNU sed to delete one or two 0's after a word boundary:-
Code:

echo 011.100.000.001 | sed 's/\b00\?//g'
11.100.0.1


rocket357 08-26-2008 12:55 AM

Quote:

Originally Posted by Kenhelm (Post 3259841)
This uses GNU sed to delete one or two 0's after a word boundary:-
Code:

echo 011.100.000.001 | sed 's/\b00\?//g'
11.100.0.1


very nice...thanks for sharing.


All times are GMT -5. The time now is 07:11 AM.