This is the mail archive of the mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Cygwin's vanilla sed : capabilities and limitations

Igor Pechtchanski wrote:
> On Wed, 16 Jul 2003, Igor Pechtchanski wrote:
>> On Wed, 16 Jul 2003 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)

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

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:
Problem reports:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]