Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-25-2008, 02:34 PM
|
#1
|
|
Member
Registered: Jan 2007
Location: Athlone, ROI
Distribution: Ubuntu Hardy Desktop, Solaris 10, Workstation 2008 x64
Posts: 75
Rep:
|
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
|
|
|
|
08-25-2008, 02:50 PM
|
#2
|
|
Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 11,777
|
Quote:
Originally Posted by Geneset
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?
|
|
|
|
08-25-2008, 02:59 PM
|
#3
|
|
Member
Registered: Jan 2007
Location: Athlone, ROI
Distribution: Ubuntu Hardy Desktop, Solaris 10, Workstation 2008 x64
Posts: 75
Original Poster
Rep:
|
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.
|
|
|
|
08-25-2008, 03:50 PM
|
#4
|
|
Member
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 474
Rep:
|
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.
|
|
|
|
08-25-2008, 05:05 PM
|
#5
|
|
Member
Registered: Jan 2007
Location: Athlone, ROI
Distribution: Ubuntu Hardy Desktop, Solaris 10, Workstation 2008 x64
Posts: 75
Original Poster
Rep:
|
I could live with python...
Go on....
|
|
|
|
08-25-2008, 05:49 PM
|
#6
|
|
Member
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 474
Rep:
|
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.
|
|
|
|
08-25-2008, 05:56 PM
|
#7
|
|
Member
Registered: Jan 2007
Location: Athlone, ROI
Distribution: Ubuntu Hardy Desktop, Solaris 10, Workstation 2008 x64
Posts: 75
Original Poster
Rep:
|
Simple, Elegant, I like it *steals*
Thanks for all the help guys 
|
|
|
|
08-26-2008, 12:53 AM
|
#8
|
|
Member
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 323
Rep: 
|
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
|
|
|
|
08-26-2008, 12:55 AM
|
#9
|
|
Member
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 474
Rep:
|
Quote:
Originally Posted by Kenhelm
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:13 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|