LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-21-2008, 11:05 AM   #1
chess
Member
 
Registered: Mar 2002
Location: 127.0.0.1
Distribution: Slackware, OpenBSD, FreeBSD
Posts: 728

Rep: Reputation: 168Reputation: 168
insert zero before single numbers using sed (or awk)


I have been banging my head and googling for about a week trying various things and cannot come up with a solution. So, I would really appreciate any assistance.

I need to add a zero before a single number surrounded by period. For example:

10.4.256.14.3 would need to be converted to 10.04.256.14.03
3.4 would need to be converted to 3.04

I cannot just add a zero before all numbers, only the 'single' digits surrounded by periods.

Here is what I have so far:

Code:
#!/bin/bash
ONE=10.4.256.14.3
TWO=$(echo $ONE | sed -e 's/\.\([0-9]\)\./\.0\1\./g')
echo $TWO
The output is:

10.04.236.14.3

It works for the first single number, the '4' but not for the second single number, the '3' at the end. I know the problem is the use of the \1 but I am using that because I need to 'keep' the number matched by [0-9] so I can stick it back in after inserting the 0. I hope this makes sense.

Thanks for the help.
 
Old 10-21-2008, 11:14 AM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Assuming the number of characters remains constant in each "column":
Code:
echo 10.4.256.14.3 | awk -F"." '{printf "%2i.%02i.%3i.%2i.%02i\n", $1,$2,$3,$4,$5}'
I imagine colucix or billymayday will come along with a much more elegant solution
 
Old 10-21-2008, 11:19 AM   #3
chess
Member
 
Registered: Mar 2002
Location: 127.0.0.1
Distribution: Slackware, OpenBSD, FreeBSD
Posts: 728

Original Poster
Rep: Reputation: 168Reputation: 168
Yeah, unfortunately, they don't. That's why I used the two examples, one with 5 'columns' and one with 2. There is no pattern to either the number of columns or the number of digits in any particular column.

Last edited by chess; 10-21-2008 at 11:21 AM.
 
Old 10-21-2008, 11:30 AM   #4
AuroraCA
Member
 
Registered: Jul 2008
Location: Northern CA USA
Distribution: Ubuntu, Slackware, Gentoo, Fedora, Red Hat, Puppy Linux
Posts: 370

Rep: Reputation: 35
In sed and awk '^' indicates beginning of line and '$' indicatesend of line are recognized.

The following would replace your end of line condition.
Code:
#!/bin/bash
ONE=10.4.256.14.3
TWO=$(echo $ONE | sed -e {
's/\.\([0-9]\)\./\.0\1\./g')
's/\.\([0-9]\)$/\.0\1\')
}
echo $TWO
Note: there is no g after the second replacement statement since there can only be one eol condition per line.

Last edited by AuroraCA; 10-21-2008 at 11:47 AM. Reason: Clarification
 
Old 10-21-2008, 11:45 AM   #5
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
I think you meant:

Code:
#!/bin/bash
ONE=10.4.256.14.3
TWO=$(echo $ONE | sed -e 's/\.\([0-9]\)\./\.0\1\./g' -e 's/\.\([0-9]\)$/\.0\1/')
echo $TWO
HTH

Forrest
 
Old 10-21-2008, 11:50 AM   #6
AuroraCA
Member
 
Registered: Jul 2008
Location: Northern CA USA
Distribution: Ubuntu, Slackware, Gentoo, Fedora, Red Hat, Puppy Linux
Posts: 370

Rep: Reputation: 35
Sorry if I mislead you. I'm use to doing substitutions from a file.

Thanks for the correction.
 
Old 10-21-2008, 12:03 PM   #7
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,606

Rep: Reputation: 135Reputation: 135
Maybe with perl:
Code:
$ echo 10.4.256.14.3 | perl -pe  's/\.(\d[^0-9])/.0$1/g'
10.04.256.14.03
$ echo 3.4 | perl -pe  's/\.(\d[^0-9])/.0$1/g'
3.04
 
Old 10-21-2008, 12:03 PM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
This uses GNU sed.
\b represents a word boundary.
Code:
echo 10.4.256.14.3 | sed 's/\.\([0-9]\)\b/.0\1/g'
10.04.256.14.03
 
Old 10-21-2008, 01:16 PM   #9
chess
Member
 
Registered: Mar 2002
Location: 127.0.0.1
Distribution: Slackware, OpenBSD, FreeBSD
Posts: 728

Original Poster
Rep: Reputation: 168Reputation: 168
Thanks, everybody, for the excellent help. Several of these solutions seem to work just fine. I'm testing out Kenhelm's solution in my script and so far so good.

Thanks, again!
 
Old 10-21-2008, 04:53 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,351

Rep: Reputation: 1041Reputation: 1041Reputation: 1041Reputation: 1041Reputation: 1041Reputation: 1041Reputation: 1041Reputation: 1041
Nice answer - does what was asked for. Small nit - does the OP really want ".0." converted to ".00." ???
Maybe, maybe doesn't care.

Easy to fix.
 
Old 10-22-2008, 08:06 AM   #11
chess
Member
 
Registered: Mar 2002
Location: 127.0.0.1
Distribution: Slackware, OpenBSD, FreeBSD
Posts: 728

Original Poster
Rep: Reputation: 168Reputation: 168
Quote:
Originally Posted by syg00 View Post
Nice answer - does what was asked for. Small nit - does the OP really want ".0." converted to ".00." ???
Maybe, maybe doesn't care.

Easy to fix.
No, in this case it does not matter if .0. is converted to .00. -- but point well taken, sometimes it matters a great deal. :-)

Thanks again for all the input and suggestions. It's really appreciated.
 
  


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
insert a single quote is text file with sed eln01 Programming 7 05-10-2007 11:02 AM
Insert character by using sed/awk manish_meet_in Linux - General 3 04-05-2007 12:19 PM
Insert CRLF's with sed? unSpawn Programming 6 11-22-2006 07:46 AM
SED - replace / insert furquan Programming 5 03-01-2006 06:58 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


All times are GMT -5. The time now is 10:29 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