Different use of hold space with POSIX sed... to load the entire file into the hold space before merging lines.
sed -n '1x;1!H;${g;s/\n\s*+//g;p}'
1x
on the first line, swap the line into the empty hold space
1!H
on non-first lines, append to the hold space
$
on the last line:
g
get the hold space (the entire file)
s/\n\s*+//g
replace newlines preceeding +
p
print everything
Input:
line 1
line 2
+ continue 2
+ continue 2 even more
line 3
+ continued
becomes
line 1
line 2 continue 2 continue 2 even more
line 3 continued
This (or potong's answer) might be more interesting than a sed -z
implementation if other commands were desired for other manipulations of the data you can simply stick them in before 1!H
, while sed -z
is immediately loading the entire file into the pattern space. That means you aren't manipulating single lines at any point. Same for perl -0777
.
In other words, if you want to also eliminate comment lines starting with *
, add in /^\s*\*/d
to delete the line
sed -n '1x;
/^\s*\*/d;
1!H;${g;s/\n\s*+//g;p}'
versus:
sed -z 's/\n\s*+//g;
s/\n\s*\*[^\n]*\n/\n/g'
The former's accumulation in the hold space line by line keeps you in classic sed line processing land, while the latter's sed -z
dumps you into what could be some painful substring regexes.
But that's sort of an edge case, and you could always just pipe sed -z
back into sed. So +1 for that.
Footnote for internet searches: This is SPICE netlist syntax.