tobold.org

correct • elegant • free

△ comp.unix.shell △

◅ kill -9 sometimes required

grep behaves differently in find ▻

using sed substitute command

In article <qc1n1qoyf57.fsf@sirppi.helsinki.fi>,
Ari K Makela  <hauva@iki.fi> wrote:
>[/tmp]$ perl -pe 's/ki.*?\s+/boys /' foo
>there are three boys on the street

Ugh.  What does this do when `kids' is at the end of a line?  At the
end of the file?

It's pretty crummy to match any whitespace characters, then blindly
replace them with a space.  So, still in Perl mode, a better version
would be this.

    perl -pe 's/ki.*?(\s)/boys$1/' foo

Why do we need to match the whitespace, only to replace it by itself,
in the first place?  Because we've decided that non-greedy regexps are
the right approach, and we need the context to make them work.

A solution already posted,

    sed 's/ki[a-z]*/boys/' foo

achieves the same effect, without using non-greedy regexps, in sed
(or perl, for that matter).

>With the question mark the regexp is not greedy and matches the
>shortest possible string. Note that extended regular expressions are
>needed in this solution and that is not something that sed can do.

As we've shown, extended regexps are not needed here.  (In my
experience, it's very rare in practical applications that you need the
power of non-greedy regexps, although they are often easier to construct
than a greedy equivalent.)

Tim.
--
Tim Goodwin   | "If you don't know what closures are, you probably don't
Leicester, UK | want to know what closures are." -- Larry Wall

Original headers:

From: tjg@star.le.ac.uk (Tim Goodwin)
Newsgroups: comp.unix.shell
Subject: Re: using sed substitute command
Date: 7 Jan 2000 17:10:43 -0000
Message-ID: <8556n9$6ie$1@ltpcg.star.le.ac.uk>
References: <386F1864.7EB1E01C@emc.co.il>
  <qc1n1qoyf57.fsf@sirppi.helsinki.fi>

△ comp.unix.shell △

◅ kill -9 sometimes required

grep behaves differently in find ▻