LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   convert simple ip address to 3 digit octet (https://www.linuxquestions.org/questions/linux-newbie-8/convert-simple-ip-address-to-3-digit-octet-738323/)

don_wombat_73 07-07-2009 09:22 AM

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


jdkaye 07-07-2009 09:48 AM

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

MensaWater 07-07-2009 09:58 AM

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.

onebuck 07-07-2009 10:08 AM

Hi,

It does sound like homework to me.

don_wombat_73 07-07-2009 10:09 AM

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...

don_wombat_73 07-07-2009 10:11 AM

heh Not homework, onebuck. just trying to get a dhcpd.conf file worked out the way I need it to.

ghostdog74 07-07-2009 10:13 AM

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

onebuck 07-07-2009 10:29 AM

Hi,
Quote:

Originally Posted by don_wombat_73 (Post 3599743)
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.

don_wombat_73 07-07-2009 10:30 AM

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


don_wombat_73 07-07-2009 10:38 AM

no offense taken, ondbuck...
Just showing my poor linux scripting skills...

Kenhelm 07-07-2009 11:02 AM

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


don_wombat_73 07-07-2009 11:08 AM

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.

MensaWater 07-07-2009 12:12 PM

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


All times are GMT -5. The time now is 09:24 AM.