Description
  • A block starts from a line beginning with <block>,
    and ends at a line beginning with </block>.
  • In the example, we want to delete the second block of the datafile.
Raw Input Desired Output
line 1
line 2
<block>
block 1, line 1
block 1, line 2
block 1, line 3
</block>
line 8
line 9
<block>
block 2, line 1
block 2, line 2
</block>
line 14
<block>
block 3, line 1
</block>
line 18
line 1
line 2
<block>
block 1, line 1
block 1, line 2
block 1, line 3
</block>
line 8
line 9
line 14
<block>
block 3, line 1
</block>
line 18
Script and Comments
Script1
[ 1] /^<block>/,/^<\/block>/!b
[ 2] /^<block>/{
[ 3] x
[ 4] s/^/#/
[ 5] x
[ 6] }
[ 7] G
[ 8] /\n#{2}$/d
[ 9] s/\n.*//
Comments -r
  1. To locate the block we want (in this case, the second one), we have to number each block by a counter, where
    • The value of the counter is stored in HS as the same number of hash signs (`#').
    • The initial value of the counter is zero.
    • Blocks are numbered from 1.
    • Each time the first line of a block is read, we increment the counter by one to get the block number.
  2. The counter is kept in HS, and the only way to edit HS is:
    • first exchange the contents of PS and HS, which can be done via command `x'.
    • then edit the contents of PS.
    • finally, exchange PS and HS again.
  3. Step [1] prints lines not belonging to any block, where command `b' without a destination label makes sed print the current line and then start a new cycle.
  4. When the first line of a block has been read to PS:
    • Step [2] matches.
    • Step [3] exchanges PS and HS.
    • Step [4] increment the counter by inserting a hash mark in the beginning.
    • Step [5] exchanges PS and HS again.
  5. Any line belonging to some block will be processed by Step [7]. At this point, HS contains the block number. To determine whether this line has to be printed or deleted,:
    • Step [7] appends to PS the block number in hash signs.
    • If this line belonging to the block to be deleted, Step [8] deletes it; otherwise, after removing the attached hash signs by Step [9], it will be printed.
  6. If several blocks have to be deleted, we can modfiy RE of Step [8] according to our needs. For example, \n(#|#{4}|#{7})$ can be used to delete block 1, 4, and 7, respectively.