tobold.org

correct • elegant • free

△ comp.unix.shell △

◅ Using n

Using n ▻

Using sed on /etc/group

In article <8800nv$mk5$1@news-int.gatech.edu>,
Jehsom <jehsom@-NOSPAM-resnet.gatech.edu> wrote:
>[jehsom@nullity /etc]$ foo=bar sed 's/^\(all:.*:[0-9]+:\)/\1${foo},/g' group

sed only supports Basic Regular Expressions, so doesn't have the `+'
operator.  In some versions of sed it's available as `\+', but for this
case `[0-9][0-9]*' does perfectly well.

Your other problem is that variable substitution doesn't occur inside
single quotes: if you fix the first problem, you'll end up with a
literal `${foo}' in your file.  You could use double quotes, or just end
the quotation (I prefer the latter, of course :-).

We can also tidy up a bit.  Since your `\1' refers to the entire left
hand side, you can use `&' and remove the parentheses.  Also, the `g'
is redundant: a match anchored to the beginning of the line can only
ever match once.  Finally, I'd be inclined to match the second field
with `[^:]*', rather than `.*'; I guess this comes under the heading
Defensive Programming.

    foo=bar sed 's/^all:[^:]*:[0-9][0-9]*:/&'$foo',/' group

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 on /etc/group
Date: 11 Feb 2000 12:17:23 -0000
Message-ID: <880ul9$ubu$1@ltpcg.star.le.ac.uk>
References: <8800nv$mk5$1@news-int.gatech.edu>

△ comp.unix.shell △

◅ Using n

Using n ▻