delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2003/10/20/18:54:51

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
From: "Hannu E K Nevalainen" <garbage_collector AT telia DOT com>
To: "ML CygWIN" <cygwin AT cygwin DOT com>
Subject: RE: OT: Using sed - guru help wanted.
Date: Tue, 21 Oct 2003 00:54:27 +0200
Message-ID: <NGBBLLIAMFLGJEOAJCCEEEIPDFAA.garbage_collector@telia.com>
MIME-Version: 1.0
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
Importance: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
In-Reply-To: <3F9448F1.91AFE2F3@dessent.net>

> From: Brian Dessent
> Sent: Monday, October 20, 2003 10:43 PM

> Hannu E K Nevalainen wrote:
>
> > $ echo ' a b / c d e ' | \
  sed -e 's/ *\(.*\) *\/ *\(.*\) */.\1.\2./'
> >
> > .a b .c d e .
> >
> > I want the output to be '.a b.c d e.' - that is; strip out the
> > trailing spaces.
> >
> > HOW do I achieve that?  ( \s = any ws, \S = any non ws )
> >
> > Obviously \(.*\) grabs/includes the last space. My brain has stoppped
> > working, so right now I can't work around that :-I
>
> Personally, I can't stand the "basic" style regexps that sed uses, and
> I prefer perl-compatible.  You can use the non-greedy modifier, for
> example:
>
> command | perl -pe 's!^ +(.*) +/ +(.*?) +$!.\1.\2.!'

 Thanks a lot!  Now all I have to do is understand where the difference is
;-7


$ echo ' a b / c d e ' | \
  sed -re 's!^ +(.*) +/ +(.*?) +$!.\1.\2.!'
.a b.c d e.

It seems as the -r flag to sed made _this_ expression work right OOTB.
Hmm... lets see;

$ echo ' a b / c d e ' |
  sed -e 's/^ \+\(.*\) \+\/ \+\(.*\) \+$/.\1.\2./'
.a b.c d e.

So THERE was the problem, some escaping needed.

As it seems my query wasn't that well formed...  i.e. remove any leading
and/or trailing spaces on the parts. Parts separated by the slash.
This seems to do exactly what I'm after;

$ echo 'a b/c d e  ' | \
  sed -re 's- *(.*[^ ]) */ *(.*[^ ]) *$-.\1.\2.-'

Thanks for the input, Brian and Igor.

/Hannu E K Nevalainen, B.Sc. EE - 59?16.37'N, 17?12.60'E
-- printf("Timezone: %s\n", (DST)?"UTC+02":"UTC+01"); --
--END OF MESSAGE--


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019