Sed multiline that works for hyphenating ABCABC
at an arbitrary position:
$ sed -r 'N;s/A(=\n)?B(=\n)?C(=\n)?A(=\n)?B(=\n)?C/D\1E\2F\3D\4E\5F/g;P;D' infile
BLABLD AEFDEF blabl=
a blabla blabla DEF=
DEF blabla blabla D=
EFDEF blabla
N;P;D
is the idiomatic way of keeping two lines at a time in the pattern space. The substitution checks for ABCABC
optionally interspersed with =
and a newline at any position, and the substitution inserts back what was captured.
This requires extended regular expressions (-E
in BSD sed) for the ?
operator. GNU sed supports \?
in BRE as an extension, though, but all the ()
would have to be escaped as well.
In case the =
just symbolizes a newline and isn't actually there, this simplifies to
$ sed -r 'N;s/A(\n?)B(\n?)C(\n?)A(\n?)B(\n?)C/D\1E\2F\3D\4E\5F/g;P;D' infile
BLABLA DEFDEF blabl
a blabla blabla DEF
DEF blabla blabla D
EFDEF blabla