manpagez: man pages & more
info sed
Home | html | info | man

File: sed.info,  Node: Joining lines,  Next: Centering lines,  Up: Examples

7.1 Joining lines
=================

This section uses ‘N’, ‘D’ and ‘P’ commands to process multiple lines,
and the ‘b’ and ‘t’ commands for branching.  *Note Multiline
techniques:: and *note 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: 
     From: John Doe 
     To: Jane Smith 

     $ 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: 
     From: John Doe 
     To: Jane Smith 

     # A portable (non-gnu) variation:
     #   sed -e :a -e '$!N;s/\n  */ /;ta' -e 'P;D'

© manpagez.com 2000-2026
Individual documents may contain additional copyright information.