LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-07-2009, 09:22 AM   #1
don_wombat_73
Member
 
Registered: Sep 2005
Posts: 60

Rep: Reputation: 15
convert simple ip address to 3 digit octet


I have been searching for a while online how to do this, but can't find anything or any examples.

if I had a file full of ip address, what would be the best method for converting them to 3 digit octets?

input:
Code:
fixed-address 1.2.3.4
fixed-address 22.33.123.234
output:
Code:
fixed-address 001.002.003.004
fixed-address 022.033.123.234
 
Old 07-07-2009, 09:48 AM   #2
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,464

Rep: Reputation: Disabled
It's called "left zero padding" and you can do it in just about any programming language. Here's an example of someone doing it in bash:
http://jonathanwagner.net/2007/04/zero-padding-in-bash/
but you could do it in perl, python, icon, etc., etc.
If you do programming at all, I guess you know this. Just grab the manual of your language/script of choice and you're sure to find a pad function.
Cheers,
jdk
 
Old 07-07-2009, 09:58 AM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,837
Blog Entries: 14

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
This would work:

Code:
while read label address
do echo $address |awk -F. '{printf "fixed-address %03d.%03d.%03d.%03d\n",$1,$2,$3,$4}'
done <file
Where "file" is the name of the file that contains these values.
Note in the above I have hard coded "fixed-address" as that is common to both your lines. If it weren't you'd have to modify the printf to get the value read from $label.
 
Old 07-07-2009, 10:08 AM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,716
Blog Entries: 27

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
Hi,

It does sound like homework to me.
 
Old 07-07-2009, 10:09 AM   #5
don_wombat_73
Member
 
Registered: Sep 2005
Posts: 60

Original Poster
Rep: Reputation: 15
Hey jdkaye,

Thanks for the link! I'll look through it.

I think my biggest problem is going to be trying to find the middle of the string though...
 
Old 07-07-2009, 10:11 AM   #6
don_wombat_73
Member
 
Registered: Sep 2005
Posts: 60

Original Poster
Rep: Reputation: 15
heh Not homework, onebuck. just trying to get a dhcpd.conf file worked out the way I need it to.
 
Old 07-07-2009, 10:13 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
bash
Code:
while read label address
do
    OFS=$IFS
    IFS="."
    set -- $address
    printf "%03d.%03d.%03d.%03d\n" $1 $2 $3 $4
    IFS=$OFS
done < file
awk
Code:
awk '{  m=split($2,a,"."); printf "%03d.%03d.%03d.%03d\n",a[1],a[2],a[3],a[4]}' file
 
Old 07-07-2009, 10:29 AM   #8
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,716
Blog Entries: 27

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
Hi,
Quote:
Originally Posted by don_wombat_73 View Post
heh Not homework, onebuck. just trying to get a dhcpd.conf file worked out the way I need it to.
Not meaning to offend but padding along with the IP example was a simple homework problem. Sorry.
 
Old 07-07-2009, 10:30 AM   #9
don_wombat_73
Member
 
Registered: Sep 2005
Posts: 60

Original Poster
Rep: Reputation: 15
Thanks ghostdog,

Your script got me close. I need it to only run on lines matching 'fixed-address'. So I modified the script to this:

Code:
awk '
/^fixed/
{
  m=split($2,a,"."); printf "%03d.%03d.%03d.%03d\n",a[1],a[2],a[3],a[4]
}' file
Now the output looks like:
Code:
fixed-address 10.10.3.30;
010.010.003.030
000.000.000.000
000.000.000.000
000.000.000.000
000.000.000.000
000.000.000.000
000.000.000.000
000.000.000.000
fixed-address 10.10.6.28;
010.010.006.028
000.000.000.000
000.000.000.000
000.000.000.000
000.000.000.000
000.000.000.000
000.000.000.000
000.000.000.000
 
Old 07-07-2009, 10:38 AM   #10
don_wombat_73
Member
 
Registered: Sep 2005
Posts: 60

Original Poster
Rep: Reputation: 15
no offense taken, ondbuck...
Just showing my poor linux scripting skills...
 
Old 07-07-2009, 11:02 AM   #11
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
This only works with GNU sed as it needs the 'e' command to execute a shell command in the pattern space.
Code:
sed '/^fixed-address/{
s/\./ /g
s/.*/printf "%s %03d.%03d.%03d.%03d\n" &/e
}' file
 
Old 07-07-2009, 11:08 AM   #12
don_wombat_73
Member
 
Registered: Sep 2005
Posts: 60

Original Poster
Rep: Reputation: 15
Kenhelm, you are my hero! That did EXACTLY what I needed it to do...

I appreciate all of your help. I'm still learning some of these things, and appreciate the learning experience that you all have been able to give.
 
Old 07-07-2009, 12:12 PM   #13
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,837
Blog Entries: 14

Rep: Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092Reputation: 1092
Question

Thanks for everyone but me. Did I do something to you?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Convert ip address from char* to unsigned int* Kunsheng Programming 6 04-29-2009 02:12 PM
how to convert address in current process to filename:line number telia Linux - General 5 01-12-2009 04:06 AM
Xen and the art of IP address assignment--missing something simple? JMCraig Red Hat 1 11-20-2008 11:54 AM
Convert Sector Address to File - reiserfs MQMan Linux - Hardware 1 04-04-2007 10:19 PM
xdm error : Cannot convert Internet address to host name lovelinux Linux - Networking 2 06-01-2005 09:31 PM


All times are GMT -5. The time now is 12:38 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration