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 Code:
fixed-address 001.002.003.004 |
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 |
This would work:
Code:
while read label address 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. |
Hi,
It does sound like homework to me. |
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... |
heh Not homework, onebuck. just trying to get a dhcpd.conf file worked out the way I need it to.
|
bash
Code:
while read label address Code:
awk '{ m=split($2,a,"."); printf "%03d.%03d.%03d.%03d\n",a[1],a[2],a[3],a[4]}' file |
Hi,
Quote:
|
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 ' Code:
fixed-address 10.10.3.30; |
no offense taken, ondbuck...
Just showing my poor linux scripting skills... |
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/{ |
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. |
Thanks for everyone but me. Did I do something to you?
|
All times are GMT -5. The time now is 09:24 AM. |