Sponsored Content
Top Forums Shell Programming and Scripting Replace one digit by two digit using sed Post 302186627 by Jae on Thursday 17th of April 2008 03:27:43 PM
Old 04-17-2008
Looks simple, but..

Quote:
Originally Posted by era
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.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

why "expr "${REPLY}" : '\([1-9][[:digit:]]*\)" can figure out whether it is a digit?

I found below script to check whether the variable is a digit in ksh. ############################ #!/bin/ksh REPLY="3f" if ]*\)'` != ${REPLY} && "${REPLY}" != "0" ]] then print "is digit\n" else print "not digit\n" fi ############################ Although it works fine, but... (6 Replies)
Discussion started by: sleepy_11
6 Replies

2. Shell Programming and Scripting

How to convert a 2 digit to 4 digit

Hi All, How can i convert a number 24 to 0024 In the same way how can i convert 123 to 0123? All this has to be done inside a script Thanks in advance JS (6 Replies)
Discussion started by: jisha
6 Replies

3. Shell Programming and Scripting

Single digit date to double digit date.

I have a var storing date var=`date` Now the date is returned as Mon Feb 2 00:25:48 PST 2009 Is there any way to check the date field alone ("2" in above case) and if its a single digit then add a prefix 0 to it and store the result in same variable "var" My intention in above case is... (3 Replies)
Discussion started by: villain41
3 Replies

4. Shell Programming and Scripting

sed - delete everything until the first digit comes up

Hi, I have a text file with content as follows: bla foo3200492 comment on this: 3900302 here comes the teddy 12 all I need is: 3200492 3900302 12 So I need the correct "sed" command to delete everything until the first number. I am not a regex expert, because I have to... (3 Replies)
Discussion started by: mcW
3 Replies

5. Shell Programming and Scripting

Place digit in front of the line searching pattern using sed command

hi All, i want to add the single digit front of the line in the report file and string compare with pattern file. patter file: pattern1.txt pattern num like 4 love 3 john 2 report file: report.txt i like very much but john is good boy i will love u so after execute... (9 Replies)
Discussion started by: krbala1985
9 Replies

6. Shell Programming and Scripting

awk length of digit and print at most right digit

Have columns with digits and strings like: input.txt 3840 3841 3842 Dav Thun Tax Cahn 146; Dav. 3855 3853 3861 3862 Dav Thun Tax 2780 Karl VI., 3873 3872 3872 Dav Thun Tax 3894 3893 3897 3899 Dav Thun Tax 403; Thun 282. 3958 3959 3960 Dav Thun Tax 3972 3972 3972 3975 Dav Thun Tax... (8 Replies)
Discussion started by: sdf
8 Replies

7. Shell Programming and Scripting

convert two digit in to single digit...

Hi Guys. My Input: ABCD 12 00 KL ABCD 12 08 DL ABCD 12 10 KK ABCD 12 04 LL ABCD 13 00 LP ABCD 13 1O LS Output: ABCD 12 0 KL ABCD 12 8 DL ABCD 12 10 KK ABCD 12 4 LL ABCD 13 0 LP (2 Replies)
Discussion started by: pareshkp
2 Replies

8. Shell Programming and Scripting

problem using pattern with two digit in sed

Hi, I am trying to create a csv from a existing flat file.I am using the same data to create the csv file. But I have issues \10 columns onwards.. sed... (5 Replies)
Discussion started by: babom
5 Replies

9. UNIX for Dummies Questions & Answers

sed command that deletes lines with 3-5 digit strings

Hello! My final exam in my Linux class is tomorrow, and I was just reviewing the grep and sed commands. I came across an example, by which I got stumped: it asks to provide a sed command that deletes all lines that contain exactly 3 to 5 digit strings, from a file. In this case, I created a... (3 Replies)
Discussion started by: kalpcalp
3 Replies

10. UNIX for Beginners Questions & Answers

Cut first value after underscore and replace first two digit with zero in perl

Like I have below string XX_49154534_491553_201_122023_D XX_49159042_491738_201_103901_D and the expected output would be 0154534 0159042 XX and 49 can be dynamic. (1 Reply)
Discussion started by: nadeemrafikhan
1 Replies
CheckDigits::M09_001(3pm)				User Contributed Perl Documentation				 CheckDigits::M09_001(3pm)

NAME
CheckDigits::M09_001 - compute check digits for Euro notes SYNOPSIS
use Algorithm::CheckDigits; $euro = CheckDigits('euronote'); if ($euro->is_valid('X07738250357')) { # do something } $cn = $euro->complete('X0773825035'); # $cn = 'X07738250357' $cd = $euro->checkdigit('X07738250357'); # $cd = '7' $bn = $euro->basenumber('X07738250357'); # $bn = 'X0773825035' DESCRIPTION
ALGORITHM 1. Letters are replaced with their position in the alphabet ('A' = 1, ...). 2. The total of the digits of all numbers is computed. 3. This sum is taken modulo 9. 4. The check digit is the difference between 8 and the number of step 3. To validate the last digit of the total of the digits of all numbers inclusive check digit must be 8. METHODS is_valid($number) Returns true only if $number consists solely of numbers and the last digit is a valid check digit according to the algorithm given above. Returns false otherwise, complete($number) The check digit for $number is computed and concatenated to the end of $number. Returns the complete number with check digit or '' if $number does not consist solely of digits. basenumber($number) Returns the basenumber of $number if $number has a valid check digit. Return '' otherwise. checkdigit($number) Returns the checkdigit of $number if $number has a valid check digit. Return '' otherwise. EXPORT None by default. AUTHOR
Mathias Weidner, <mathias@weidner.in-bad-schmiedeberg.de> SEE ALSO
perl, CheckDigits, www.pruefziffernberechnung.de. perl v5.10.0 2008-05-17 CheckDigits::M09_001(3pm)
All times are GMT -4. The time now is 06:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy