>> I am using sed with cygwin.
>> I am trying to use groups for a replacement
>> ie:
>> change the string:
>> 1252345.125345.125345
|
+---where did this 2 go to? OP: be more consistent with examples
or is that really part of the problem, badly described?
be careful! an IMPORTANT part of programming!
Quote:>> to:
>> 129345.129345.129345
>> replace 5' preceded by a 2 with a 9
>> I thought the following should to it:
>> sed s/\(2\)\(5\)/\29/g
>> This does not seem to work. The string is not changed.
>> so what the hell am I doing wrong.
Is the shell eating the backslash escapes? Do you need some quotes? or
double up on the \ so that one of them gets through the shell to sed? In
any case the syntax is wrong. You should really cut/paste from your screen
session if at all possible. I usually keep the shell prompt ($) also, to
make it easier for others to read/understand what it is. For example (on
my SuSE 9.1 Linux system):
bash-2.05b$ echo 1252345.125345.125345|sed "s/252*/29/g"
129345.129345.129345
At first I thought you always needed the -e switch for sed, but I have
learned to always check the man pages (before spouting off on internet).
Quote:> It is not quite clear whether you want 's/25/9/' or
> 's/25/29/' but you can do these trivially without
> using \(\) and so on.
> What you are doing with 's/\(2\)\(5\)/\29/g' is
> replacing each 25 -- \(2\)\(5\) -- with the second
> thing matched -- \2 -- (which is 5) followed by a 9.
> So you are replacing each 25 with 59. You perhaps
> meant \1 (which is the first thing matched, or 2).
> It might be a bit clearer with letters rather than numbers:
> If there is any digit followed by an e or f, place an X
> between them:
> $ echo abcd4ef.abc6def.abcde9f |sed 's/\([0-9]\)\([ef]\)/\1X\2/g'
> abcd4Xef.abc6def.abcde9Xf
Yeah, this is a much better example. It has variability in matched pattern.
I would emphasize that there is really no point in using matched patterns,
unless you have some variability in them, i.e. you do not know a priori
exactly what the matched string(s) will be. Otherwise, it is much easier,
and usually less error prone to just use the exact string to be matched,
both in pattern and substitution. Besides, I am lazy. If this was a
homework assignment example, IMO it was poorly chosen. Not illustrative.
The pattern match/substitution capability is a powerful and useful
facility, but usually it is overkill. In the OP case (his verbal
description and 1st cut solution attempt), a simple
s/25/29/g
would do the trick. Why try to get fancy (and stumble, as I often do).
BTW, from the example (not the word description) my impression is that the
OP really wants
s/252\?/29/g
which should match 25 or 252 (i.e. optional 2 suffix) and replace entire
matched pattern by 29. That is what his example shows (but not what his
words describe). I might be wrong. In any case, the problem STILL does not
require groups to solve it. BTW, my 1st cut used 2*, not 2\?. Who knows
what the real problem is/was? Whatever...
--
Juhan Leemet
Logicognosis, Inc.