On Wednesday 26 September 2007, Donnie Berkholz wrote:
> On 00:02 Wed 26 Sep , Stephen Bennett wrote:
> > On Tue, 25 Sep 2007 11:10:34 +0200
> >
> > Robert Buchholz <[EMAIL PROTECTED]> wrote:
> > > I already wondered a while back:
> > > sed only fails if the file does not exist, but not if there was no
> > > replacement. Is there any way to force it to?
> >
> > Off the top of my head...
> >
> > sed -e '1{x;s/^/0/;x;ta;:a}'
> > -e 's/$STRING/$REPLACEMENT/'
> > -e 'Tb;x;s/^/1/;x;:b;${p;x;/^0/Q1;Q0};'
>
> I spent a few minutes trying to decipher that without luck. Could you
> walk me through it?
sed maintains two buffers - pattern and hold (which start out empty) and
that's the trick here
{ } - used to group commands together
1 - only match first line (it's an address match)
x - swap pattern space and hold space
s/^/0/ - turn the (now) empty pattern space into "0"
x - swap pattern space and hold space
ta - branch to label a if previous expression matched something
a: - the actual label "a" ... needed to reset branching conditions
Tb - branch to label b if previous expression matched something
x - swap pattern space and hold space
s/^/1/ - insert "1" into the pattern space
x - swap pattern space and hold space
:b - the actual label "b'
$ - only match the last line (it's an address match)
p - print current pattern space
x - swap pattern space and hold space
/^0/Q1;Q0 - if the pattern space starts with a 0, exit with 1 ... otherwise
continue on to the exit with 0 ... either way, quit without printing
the optional argument to Q is a GNU extension which isnt documented in the
manpage :( ... guess i'll send them a patch
pretty sure the first expression can be dropped:
sed -e 's/$STRING/$REPLACEMENT/' \
-e 'tb;x;s/^/1/;x;:b;${p;x;/^$/Q1;Q0};'
and the printing makes it a little reliant on how sed is used ... i think
something like this should work with -n:
-e 'Tb;x;s/^/1/;x;:b;${x;/^$/{x;q1};x;q0}'
-mike
signature.asc
Description: This is a digitally signed message part.
