Subject: Re: Cygwin's vanilla sed : capabilities and limitations
From: "Peter S Tillier" <[email-masked]>
To: <[email-masked]>
Cc: <[email-masked]>
Date: Thu, 24 Jul 2003 02:32:56 +0100

Igor Pechtchanski wrote:
> On Wed, 16 Jul 2003, Igor Pechtchanski wrote:
>
>> On Wed, 16 Jul 2003 [email-masked] 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/