LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 10-28-2010, 04:43 AM   #1
frater
Member
 
Registered: Jul 2008
Posts: 106

Rep: Reputation: 23
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

Last edited by frater; 10-28-2010 at 04:49 AM.
 
Old 10-28-2010, 04:58 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,506

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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!
 
Old 10-28-2010, 05:14 AM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
Using bash
Code:
printf '%03d.%03d.%03d.%03d'  ${IP//./ }
 
Old 10-28-2010, 06:52 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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.
 
Old 10-28-2010, 07:25 AM   #5
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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 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..

Last edited by GrapefruiTgirl; 10-28-2010 at 08:08 AM.
 
Old 10-28-2010, 10:06 AM   #6
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
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'
 
Old 10-29-2010, 08:56 AM   #7
frater
Member
 
Registered: Jul 2008
Posts: 106

Original Poster
Rep: Reputation: 23
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)

Last edited by frater; 10-29-2010 at 09:13 AM.
 
Old 10-29-2010, 08:59 AM   #8
frater
Member
 
Registered: Jul 2008
Posts: 106

Original Poster
Rep: Reputation: 23
Quote:
Originally Posted by colucix View Post
I'm sure this will become a shortest command line challenge in a couple of hours!
It seems you're right....
Keep them coming!!

Last edited by frater; 10-29-2010 at 09:07 AM.
 
  


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
PCI-BUG #81 [<some zeroes>] Found! What should I do? algogeek Linux - Hardware 3 04-29-2008 02:11 AM
payload is padded with zeros ahm_irf Linux - Networking 1 11-11-2007 06:07 AM
Date command - month of year, blank padded? menator Programming 3 06-27-2006 06:00 AM
pflogsumm generates zeroes for results rioguia Linux - Software 1 09-29-2004 03:29 PM
How do I print using format that padded space between printed value ? Linh Programming 2 06-18-2004 03:22 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration