On Mon, Feb 21, 2011 at 04:15:56PM -0800, Rich Shepard wrote:
> On Mon, 21 Feb 2011, Rich Shepard wrote:
> 
> >   In English, I want to locate one or more digits followed by a comma and
> > one or more digits, then replace only the comma with a pipe.  What I've
> > tried include:
> >     s/.*[0-9],[[0-9]/\&|/g
> 
>    More searching with Google lead me to this:
> 
> "The section explains how to do the apparently complex task of moving text
> around within lines. Consider, for example, the output of ls: say you want
> to automatically strip out only the size column-- sed can do this sort of
> editing if you use the special \( \) notation to group parts of the regular
> expression together. Consider the following example:
> 
> 
>       sed -e 's/\(<[^ ]*>\)\([ ]*\)\(<[^ ]*>\)/\3\2\1/g'
> 
> "Here sed is searching for the expression \<.*\>[ ]*\<.*\>. From the chapter
> on regular expressions, we can see that it matches a whole word, an
> arbitrary amount of whitespace, and then another whole word. The \( \)
> groups these three so that they can be referred to in <replace-text>. Each
> part of the regular expression inside \( \) is called a subexpression of the
> regular expression. Each subexpression is numbered--namely, \1, \2, etc.
> Hence, \1 in <replace-text> is the first \<[^ ]*\>, \2 is [ ]*, and \3 is
> the second \<[^ ]*\>."
> 
>    So, I tried
> 
>       s/\(.*[0-9]\),\([0-9]\)/\1|\2/g
> 
> but nothing was changed. Perhaps closer, but still not there.
 
First, since your're interested in <DIGIT>,<DIGIT> why match anything else?

Second, I don't do sed, so here's how it can be done in perl


#!/usr/bin/perl

while(<DATA>) {
    s/(\d),(?=\d)/$1|$2/g;
    print;
}

__DATA__
work with 19,43  or so
14,99
we have, at most, 14,23 to do though some say it is 18,44
Without digits, all the , will remain.
With digits 9, Ah ha! or
With,3 no change for a 2nd line
But again 3,4,5,6 all have bars.
^^^^^^^  produces vvvvvvvvvvvvvv
work with 19|43  or so
14|99
we have, at most, 14|23 to do though some say it is 18|44
Without digits, all the , will remain.
With digits 9, Ah ha! or
With,3 no change for a 2nd line
But again 3|4|5|6 all have bars.

Which I believe covers the cases you described. 
Cut #!/usr/bin/perl through the line starting }
remove the DATA text and you can run it on your data.


-- 
      Michael Rasmussen, Portland Oregon  
  Trading kilograms for kilometers since 2003
    Be appropriate && Follow your curiosity
          http://www.jamhome.us/
The Fortune Cookie Fortune today is:
Do something nice for someone, somewhere. We are all trying to make our
way in the world and kindness is never wasted.
        ~  Kent Peterson
_______________________________________________
PLUG mailing list
[email protected]
http://lists.pdxlinux.org/mailman/listinfo/plug

Reply via email to