LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   IP with padded zeroes in bash (https://www.linuxquestions.org/questions/linux-software-2/ip-with-padded-zeroes-in-bash-840919/)

frater 10-28-2010 04:43 AM

IP with padded zeroes in bash
 
To create a weblink I needed to pad zeroes to an IP
Because I couldn't find any examples I created my own.

I invite anyone to improve on it and hopefully I'll see many alternatives.

I'm using 'grep -o' to break down the 4 segments....

Code:

# IP=127.0.0.1
# PADIP=`echo "${IP}" | grep -o -E '([0-9]*\.|[0-9]*)' | awk '{printf( "%03d\n", $1)}' | tr '\n' '.' | sed 's/.$//'`
# echo ${PADIP}
127.000.000.001


colucix 10-28-2010 04:58 AM

A first improvement would be the choice of a single tool to perform the task. For example in awk:
Code:

echo $IP | awk -F. '{ OFS = FS; for ( i = 1; i <= NF; i++ ) $i = sprintf("%03d",$i); print }'
I'm sure this will become a shortest command line challenge in a couple of hours! ;)

Kenhelm 10-28-2010 05:14 AM

Using bash
Code:

printf '%03d.%03d.%03d.%03d'  ${IP//./ }

catkin 10-28-2010 06:52 AM

IP address "octets" are in decimal. Left-side padding with 0s can result in them being interpreted as octal numbers. That doesn't matter for the likes of 000 and 001 but 071 for example would be 57 in decimal.

GrapefruiTgirl 10-28-2010 07:25 AM

I found one or more problems with the examples given above (could be typos, could be system differences, who knows..)
EDIT: Must have been a death-defying series of typos on my part :scratch: as the other methods now work for me.

While this method may be no better, it appears to work for me:
Code:

echo $ip | awk -v RS="." '{if(out != ""){out = out"."}; out = out sprintf("%03u",$0)} END{print out}'
192.168.023.034

Using ORS and/or FS required extra code or just didn't work; this method isn't perfect either, but it adds variety to the thread. And, here's a sed method:
Code:

echo $ip | sed "s/\([0-9]*\).\([0-9]*\).\([0-9]*\).\([0-9]*\)/ printf "%03u.%03u.%03u.%03u" \1 \2 \3 \4 /ge"
I like Kenhelm's method the best, for its simplicity..

Kenhelm 10-28-2010 10:06 AM

Due to catkin's post I've noticed a weakness in the printf method I posted.
It can give wrong results if there are any existing leading zeros in the IP, e.g.
Code:

IP=020.030.073.0
printf '%03d.%03d.%03d.%03d' ${IP//./ }
016.024.059.000

# This GNU sed method seems to be a more general solution.
# It prepends 00 to each number, then reduces their lengths to 3.
echo $IP | sed -r 's/^|\./&00/g; s/0*([0-9]{3})/\1/g'

# or
echo 00${IP//./.00} | sed -r 's/0*([0-9]{3})/\1/g'


frater 10-29-2010 08:56 AM

I like this one:
Quote:

echo $IP | sed -r 's/^|\./&00/g; s/0*([0-9]{3})/\1/g'
And his notation is new to me:
Quote:

echo 00${IP//./.00}
Need to remember this for future use (or come back to this thread)

frater 10-29-2010 08:59 AM

Quote:

Originally Posted by colucix (Post 4141991)
I'm sure this will become a shortest command line challenge in a couple of hours! ;)

It seems you're right....
Keep them coming!!


All times are GMT -5. The time now is 06:18 PM.