It has always bugged me that sed
doesn't have a j
command, like ed
does.
This question asks how to join lines in general, and there are a bunch of good answers, many featuring other tools like awk
and perl
. And there are some sed
solutions, too, but they all involve using the s
command to literally edit out the newline, which is a solution that for various reasons strikes me as kludgey.
Now, sed
also has some obscure commands having to do with "hold" and "pattern" spaces, which I've never figured out how to use. I have always assumed -- BICBW -- that the hold space is sed's answer to the j
command, if only I could figure out how to use it.
So my question for today -- and this is as much on an intellectual as a practical question -- is, is there a good, safe, reasonably concise way to do the equivalent of
sed /pat/j
That is, for every line containing the pattern pat
, join it to the following line (that is, just as ed
would if I used g/pat/j/
).
As I said, this is somewhat of an intellectual question. I know how to join lines using other tools, or by using sed
and the s
command on a quoted literal newline. But it seems to me there ought to be a better way.
(Or maybe I should just snarf a copy of sed.c
and hack in a proper j
command.)