Replace one digit by two digit using sed | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Replace one digit by two digit using sed

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-17-2008
Jae Jae is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 14 May 2009, 11:21 AM EDT
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Replace one digit by two digit using sed

Folks,

Is there a simple way to replace one digit by two digit using sed.

Example,
mydigit1918_2006_8_8_lag1.csv should be
mydigit1918_2006_08_08_lag01.csv.

I tried this way, but doesn't work.
echo mydigit1989_2006_8_8_lag1.csv|sed 's/[[:digit:]]/0[[:digit:]]/'

Thank you,
Sponsored Links
    #2  
Old 04-17-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
Apparently your sed doesn't understand the [[:digit:]] class, but if it did, it would replace the first digit -- the 1 in 1989 -- and replace it with a literal string "[[:digit:]]". You need to supply more context; and, apparently, use the old-fashioned [0-9] construct for matching a digit.


Code:
echo mydigit1989_2006_8_8_lag1.csv | sed 's/_\([0-9]\)/_0\1/g'

The regular expression matches a single digit after an underscore, and captures it into a backreference so you can refer to it as "\1" in the substitution part. (That's what the backslashed parentheses are for. Your sed might want them without backslashes, or something.) The /g at the end says to do this as many times as possible ("globally"), not just on the first occurrence.
Sponsored Links
    #3  
Old 04-17-2008
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 15 December 2014, 12:20 PM EST
Location: USA
Posts: 1,547
Thanks: 25
Thanked 149 Times in 141 Posts

Code:
echo mydigit1918_2006_8_8_lag1.csv | sed -e 's/\([^0-9]\)\([0-9]\)\([^0-9]\)/\10\2\3/' -e 's/\([^0-9]\)\([0-9]\)\([^0-9]\)/\10\2\3/g'

    #4  
Old 04-17-2008
Jae Jae is offline
Registered User
 
Join Date: Aug 2007
Last Activity: 14 May 2009, 11:21 AM EDT
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Looks simple, but..

Quote:
Originally Posted by era View Post
Apparently your sed doesn't understand the [[:digit:]] class, but if it did, it would replace the first digit -- the 1 in 1989 -- and replace it with a literal string "[[:digit:]]". You need to supply more context; and, apparently, use the old-fashioned [0-9] construct for matching a digit.


Code:
echo mydigit1989_2006_8_8_lag1.csv | sed 's/_\([0-9]\)/_0\1/g'

The regular expression matches a single digit after an underscore, and captures it into a backreference so you can refer to it as "\1" in the substitution part. (That's what the backslashed parentheses are for. Your sed might want them without backslashes, or something.) The /g at the end says to do this as many times as possible ("globally"), not just on the first occurrence.
Thanks Era,

Your approach looks simple, but not quite right. Shamrock's method below looks long, but it gives me a right answer.
Sponsored Links
    #5  
Old 04-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
Ah yes, it gets the 2006 too. It's possible to fix that with a different regex but you already have something which works for you.
Sponsored Links
    #6  
Old 04-18-2008
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 9 November 2014, 12:24 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts
regexp can be powerful, but can get confusing for the faint hearted

Code:
# echo "mydigit1918_2006_08_08_lag01.csv" | awk -F"_" '{$3+=0;$4+=0}1' OFS="_"
mydigit1918_2006_8_8_lag01.csv

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
convert two digit in to single digit... pareshkp Shell Programming and Scripting 2 02-26-2012 02:15 PM
awk length of digit and print at most right digit sdf Shell Programming and Scripting 8 11-23-2011 10:42 AM
Single digit date to double digit date. villain41 Shell Programming and Scripting 3 02-02-2009 04:53 AM
How to convert a 2 digit to 4 digit jisha Shell Programming and Scripting 6 04-23-2008 07:38 AM
why "expr "${REPLY}" : '\([1-9][[:digit:]]*\)" can figure out whether it is a digit? sleepy_11 Shell Programming and Scripting 6 08-08-2007 04:51 AM



All times are GMT -4. The time now is 12:20 AM.