delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2003/07/23/21:34:25

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
Message-ID: <000801c35183$af605ec0$5c16989e@oemcomputer>
Reply-To: "Peter S Tillier" <peter DOT tillier AT btinternet DOT com>
From: "Peter S Tillier" <peter DOT tillier AT btinternet DOT com>
To: <fergus AT bonhard DOT uklinux DOT net>
Cc: <cygwin AT cygwin DOT com>
References: <Pine DOT GSO DOT 4 DOT 44 DOT 0307161644130 DOT 21206-100000 AT slinky DOT cs DOT nyu DOT edu>
Subject: Re: Cygwin's vanilla sed : capabilities and limitations
Date: Thu, 24 Jul 2003 02:32:56 +0100
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106

Igor Pechtchanski wrote:
> On Wed, 16 Jul 2003, Igor Pechtchanski wrote:
>
>> On Wed, 16 Jul 2003 fergus AT bonhard DOT uklinux DOT net wrote:
>>
>> > Q2. Is there a way using the supplied sed without major
>> > enhancements to change "abc x def" to "def x abc": that is, to
>> > grab two distinct portions and swap them (using $1,$2 or \1,\2 or
>> > whatever). 
>>
>> Sure.  's/^\(.*\) x \(.*\)$/'.
>
> Oops!  Make that 's/^\(.*\) x \(.*\)$/\2 x \1/'.
> Igor

Or, more efficiently, 's/^\([^ ]*\) x \(.*\)$/\2 x \1/'.  Using .* too
early in an RE causes the RE engine to do a lot of (sloooow)
backtracking.

If you say what you mean here then "everything _up to_ the first space
exchanged with everything after the second space" gives my suggestion
above.

Also from Fergus' original mail:

Q1. Querying info sed reveals the expression matcher to be "greedy",
matching the longest possible string. Is there a way to make it match
the shortest possible, so that echo aaabbbccc | sed 's/^.*b//' (altered
but similar) grabs aaab not aaabbb?

Likewise here: "everything up to the first b" is /^[^b]*b/, so you need
echo aaabbbccc | sed 's/^[^b]*b//' => bbccc

Peter S Tillier
"Who needs perl when you can write dc, sokoban,
arkanoid and an unlambda interpreter in sed?"

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