sed: Multiline techniques

1 
1 6.3 Multiline techniques - using D,G,H,N,P to process multiple lines
1 ====================================================================
1 
1 Multiple lines can be processed as one buffer using the
1 'D','G','H','N','P'.  They are similar to their lowercase counterparts
1 ('d','g', 'h','n','p'), except that these commands append or subtract
1 data while respecting embedded newlines - allowing adding and removing
1 lines from the pattern and hold spaces.
1 
1    They operate as follows:
1 'D'
1      _deletes_ line from the pattern space until the first newline, and
1      restarts the cycle.
1 
1 'G'
1      _appends_ line from the hold space to the pattern space, with a
1      newline before it.
1 
1 'H'
1      _appends_ line from the pattern space to the hold space, with a
1      newline before it.
1 
1 'N'
1      _appends_ line from the input file to the pattern space.
1 
1 'P'
1      _prints_ line from the pattern space until the first newline.
1 
1    The following example illustrates the operation of 'N' and 'D'
1 commands:
1 
1      $ seq 6 | sed -n 'N;l;D'
1      1\n2$
1      2\n3$
1      3\n4$
1      4\n5$
1      5\n6$
1 
1   1. 'sed' starts by reading the first line into the pattern space (i.e.
1      '1').
1   2. At the beginning of every cycle, the 'N' command appends a newline
1      and the next line to the pattern space (i.e.  '1', '\n', '2' in the
1      first cycle).
1   3. The 'l' command prints the content of the pattern space
1      unambiguously.
1   4. The 'D' command then removes the content of pattern space up to the
1      first newline (leaving '2' at the end of the first cycle).
1   5. At the next cycle the 'N' command appends a newline and the next
1      input line to the pattern space (e.g.  '2', '\n', '3').
1 
1    A common technique to process blocks of text such as paragraphs
1 (instead of line-by-line) is using the following construct:
1 
1      sed '/./{H;$!d} ; x ; s/REGEXP/REPLACEMENT/'
1 
1   1. The first expression, '/./{H;$!d}' operates on all non-empty lines,
1      and adds the current line (in the pattern space) to the hold space.
1      On all lines except the last, the pattern space is deleted and the
1      cycle is restarted.
1 
1   2. The other expressions 'x' and 's' are executed only on empty lines
1      (i.e.  paragraph separators).  The 'x' command fetches the
1      accumulated lines from the hold space back to the pattern space.
1      The 's///' command then operates on all the text in the paragraph
1      (including the embedded newlines).
1 
1    The following example demonstrates this technique:
1      $ cat input.txt
1      a a a aa aaa
1      aaaa aaaa aa
1      aaaa aaa aaa
1 
1      bbbb bbb bbb
1      bb bb bbb bb
1      bbbbbbbb bbb
1 
1      ccc ccc cccc
1      cccc ccccc c
1      cc cc cc cc
1 
1      $ sed '/./{H;$!d} ; x ; s/^/\nSTART-->/ ; s/$/\n<--END/' input.txt
1 
1      START-->
1      a a a aa aaa
1      aaaa aaaa aa
1      aaaa aaa aaa
1      <--END
1 
1      START-->
1      bbbb bbb bbb
1      bb bb bbb bb
1      bbbbbbbb bbb
1      <--END
1 
1      START-->
1      ccc ccc cccc
1      cccc ccccc c
1      cc cc cc cc
1      <--END
1 
11    For more annotated examples, ⇒Text search across multiple
 lines and ⇒Line length adjustment.
1