2017/09/29: Quiet ed commands

ed(1), the standard text editor, has the reputation of being a bit terse. However, today I found it actually too chatty.

I was trying (for a test) to print all the label of the "completed" messages in a text format serialisation of a sequence of protobuffers. The pedantic way to do this, would be to take all lines starting with "completed" and for each, first go up the next line starting with "id", then down to the next line containing "label", and print that. Of course, for that test, a simple g/^completed/?label?p was more than sufficient, but what would be the correct way of doing it?

The problem is that / and ? addresses cannot be chained, i.e., ?^id?/label/ is not a valid address. So we have to use the fact that g accepts a command list.


g/^completed/?^id?p\
/label/p

The above would print the correct lines, interleaved with the "id" lines (easy to filter out afterwards, so not a problem in practise either; but I still want to do it in a single ed script not modifying the file). So, basically I have to find a replacement for the first p by a command that also sets the current line but otherwise does nothing and does not print anything. Not easy: k does not set the current line and z does not accept 0 as suffix.

For GNU ed, I found a solution, as there is a comment command(!).


(.,.)#
    Begins a comment; the rest of the line, up to a newline, is ignored. If a
    line address followed by a semicolon is given, then the current address is
    set to that address. Otherwise, the current address is unchanged.

So I can have the following.
g/^completed/?^id?;#\
/label/p

But what is the portable ed command? Any comments welcome. (Just send me an email.)

PS: I had to find that out that various ed also differ in where they output the answer of the e command (implicit at startup).