From: David Neary (dneary at domain wanadoo.fr)
Date: Thu 11 Jul 2002 - 16:06:23 IST
Brian Foster wrote:
> | David Neary [ previously ] wrote:
> | > OK - this may not work, but the idea is sound enough.
> | >
> | > sed '/^\/XXX\/ CE/{d;d}' filename
> ???
> close, but not quite. ???
> yer missing a `;' here, ????????????
> after the 2nd `d' before
> the closing `}', i.e.:
Actually not, believe it or not. The semi-colon is a common, but
non-standard extension to sed to allow multiple commands on the
one line. And since the second d is the last command on the line,
the semicolon isn't necessary as a separator. The curly bracket
does need to go on a line of it's own, though.
> they are not correct because `d' starts the next cycle,
> i.e., the next line is read and the program starts from the
> beginning. hence, the 2nd `d' is never executed, and thus
> the following line is printed. but it should have been
> "removed" (not printed) .... ;-(
Wow, you're right. d discards the pattern space and loops... you
would have to use the n;n solution.
> instead, this somewhat obscure command does the trick:
>
> sed -n '\¬^/XXX/ CE¬{n;d;};p' filename
Nope - doesn't quite do the job. If you have, say, the following
/XXX/ CE
/XXX/ CE
stuff
That will print
stuff
when (by my reading of the specs) it shouldn't print anything.
I don't think -n to suppress printing could be called obscure,
though...
> | Sed commands should be on separate lines,
>
> IMHO, it's a matter of taste/style.
As I said, surprisingly not. If you're sticking to POSIX sed,
then the behaviour of ; is not defined, and the commands have to
be on different lines. In any case, the trailing curly bracket
> | and the trailing }
> | needs to be on a line of it's own. Who knew!
>
> not quite. `}' is a command and hence needs to be separated
> from the other commands, either by `;' or a newline.
It's not getting passed to the shell - this is in sed that it's
handled. And according to my man page, the closing } has to be on
it's own line.
> | sed -n '/^\/XXX\/ CE/{
> | n
> | n
> | }
> | /^\/XXX\/ CE/! p' filename
>
> close but not quite. it will fail on the input:
>
> /XXX/ CE one, 1st line
> 2nd line
> /XXX/ CE two, 3rd line
> 4th line
Ack! Yes, I was misunderstanding how the flow control worked. The
second n, changed to d, repairs it, and makes it almost
equivalent to yours. It also had the flaw that your version had.
> thanks for the corrections. I hope my comments above are also
> useful and not too misleading.
Well, we differ on some stuff :) Actually, I think this might be
the best try yet...
sed -n '
:start
/pattern/{
n
/pattern/b start
d
}
p' testfile
Maybe using a goto is cheating :) Can#t think of another way to
do it right now, though.
Dave.
--
David Neary,
Marseille, France
E-Mail: bolsh at domain gimp.org
This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:17:50 GMT