a simple sed question ( there all simple :> )

a simple sed question ( there all simple :> )

Post by Ed Morto » Sat, 13 Nov 2004 23:06:03




> I am using sed with cygwin.

> I am trying to use groups for a replacement

> ie:

> change the string:
> 1252345.125345.125345

> 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

sed 's/25/29/g'

To save and use a matched pattern (e.g. "2") you can do:

sed 's/\(2\)5/\19/g'

but that's not necessary in this case.

        Ed.

- Show quoted text -

Quote:> This does not seem to work. The string is not changed.

> so what the hell am I doing wrong.

> btw, I am quite new to sed and I been going through the o'reilly book
> with no luck. In other words I tried to RTFM.

> thanks for the help

> john

 
 
 

a simple sed question ( there all simple :> )

Post by John » Sat, 13 Nov 2004 23:26:17



> I am using sed with cygwin.

> I am trying to use groups for a replacement

> ie:

> change the string:
> 1252345.125345.125345

> 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.

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

--
John.

 
 
 

a simple sed question ( there all simple :> )

Post by Juhan Leeme » Sun, 14 Nov 2004 06:37:39




>> 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.

 
 
 

1. A simple question deserving a simple answer

: What does Unix (Specifically Linux) look like?  

A bunch of ones and zeros ;-)

: Is it like Dos? Windows?

No, thank God.

: Something else?

Yes.

There's a simple answer...

--
    Tad McClellan                          SGML Consulting
    Tag And Document Consulting            Perl programming

2. INN news server/mail/dns problems connected?

3. Simple question from simple user

4. SOUND HELP

5. Simple FTP question (very simple)

6. HELP on CERN

7. simple question about simple code

8. Restrict acces with ftp.

9. Simple question for us simple minded.

10. Simple question - simple answer?

11. *** Simple batch-style Unix question <<Answer Summary>>

12. Simple sed question

13. SIMPLE (i hope) SED QUESTION/PROBLEM