LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 08-25-2008, 02:34 PM   #1
Geneset
Member
 
Registered: Jan 2007
Location: Athlone, ROI
Distribution: Ubuntu Hardy Desktop, Solaris 10, Workstation 2008 x64
Posts: 75

Rep: Reputation: 16
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
 
Old 08-25-2008, 02:50 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Geneset View Post
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?
 
Old 08-25-2008, 02:59 PM   #3
Geneset
Member
 
Registered: Jan 2007
Location: Athlone, ROI
Distribution: Ubuntu Hardy Desktop, Solaris 10, Workstation 2008 x64
Posts: 75

Original Poster
Rep: Reputation: 16
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.
 
Old 08-25-2008, 03:50 PM   #4
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
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...

Last edited by rocket357; 08-25-2008 at 03:55 PM.
 
Old 08-25-2008, 05:05 PM   #5
Geneset
Member
 
Registered: Jan 2007
Location: Athlone, ROI
Distribution: Ubuntu Hardy Desktop, Solaris 10, Workstation 2008 x64
Posts: 75

Original Poster
Rep: Reputation: 16
I could live with python...

Go on....
 
Old 08-25-2008, 05:49 PM   #6
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
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.
 
Old 08-25-2008, 05:56 PM   #7
Geneset
Member
 
Registered: Jan 2007
Location: Athlone, ROI
Distribution: Ubuntu Hardy Desktop, Solaris 10, Workstation 2008 x64
Posts: 75

Original Poster
Rep: Reputation: 16
Simple, Elegant, I like it *steals*

Thanks for all the help guys
 
Old 08-26-2008, 12:53 AM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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
 
Old 08-26-2008, 12:55 AM   #9
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
Quote:
Originally Posted by Kenhelm View Post
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.
 
  


Reply

Tags
ip, sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
reformatting hd alex_ar Linux - Software 1 08-30-2005 12:55 PM
A little reformatting to do. Ree Linux - Newbie 16 10-04-2004 02:26 PM
Reformatting TTribaLL Linux - Newbie 1 07-06-2004 08:10 PM
Partitions and Reformatting C-Squared Linux - Newbie 4 07-08-2003 05:36 PM
Reformatting WeldT Linux - General 1 11-14-2000 07:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 02:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration