sed: Joining lines
7.1 Joining lines
=================
This section uses 'N', 'D' and 'P' commands to process multiple lines,
and the 'b' and 't' commands for branching. Multiline
techniques and Branching and flow control.
Join specific lines (e.g. if lines 2 and 3 need to be joined):
$ cat lines.txt
hello
hel
lo
hello
$ sed '2{N;s/\n//;}' lines.txt
hello
hello
hello
Join backslash-continued lines:
$ cat 1.txt
this \
is \
a \
long \
line
and another \
line
$ sed -e ':x /\\$/ { N; s/\\\n//g ; bx }' 1.txt
this is a long line
and another line
#TODO: The above requires gnu sed.
# non-gnu seds need newlines after ':' and 'b'
Join lines that start with whitespace (e.g SMTP headers):
$ cat 2.txt
Subject: Hello
World
Content-Type: multipart/alternative;
boundary=94eb2c190cc6370f06054535da6a
Date: Tue, 3 Jan 2017 19:41:16 +0000 (GMT)
Authentication-Results: mx.gnu.org;
dkim=pass header.i=@gnu.org;
spf=pass
Message-ID: <abcdef@gnu.org>
From: John Doe <jdoe@gnu.org>
To: Jane Smith <jsmith@gnu.org>
$ sed -E ':a ; $!N ; s/\n\s+/ / ; ta ; P ; D' 2.txt
Subject: Hello World
Content-Type: multipart/alternative; boundary=94eb2c190cc6370f06054535da6a
Date: Tue, 3 Jan 2017 19:41:16 +0000 (GMT)
Authentication-Results: mx.gnu.org; dkim=pass header.i=@gnu.org; spf=pass
Message-ID: <abcdef@gnu.org>
From: John Doe <jdoe@gnu.org>
To: Jane Smith <jsmith@gnu.org>
# A portable (non-gnu) variation:
# sed -e :a -e '$!N;s/\n */ /;ta' -e 'P;D'