LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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: 121

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
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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: 360

Rep: Reputation: 170Reputation: 170
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: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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: 360

Rep: Reputation: 170Reputation: 170
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: 121

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: 121

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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

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
Open Source Consulting | Domain Registration