Description
  • A block begins with a begin line and ends at a end line.
  • We want to join all lines except the last one of a block to one line.
Raw Input Desired Output
.......
.......
.......
begin
..data 1-1
..data 1-2
end
.......
.......
begin
..data 2-1
..data 2-2
..data 2-3
end
.......
.......
.......
.......
.......
begin ..data 1-1 ..data 1-2
end
.......
.......
begin ..data 2-1 ..data 2-2 ..data 2-3
end
.......
.......
Script and Comments
Script1
[ 1] /^begin$/!b
[ 2] :loop0
[ 3] $!{
[ 4] N
[ 5] /\nend$/!b loop0
[ 6] }
[ 7] :loop1
[ 8] s/\n([^\n]*\n)/ \1/g
[ 9] t loop1
Comments
  1. The `-r' options of GNU sed must be used or we have to escape parenthesis used in Step [8].
  2. If a line does not belong to any block, Step [1] will print it and then starts a new cycle.
  3. The loop consisting of Steps [2] thru [6] will read all lines of a block to the Pattern Space.
  4. Steps [7] thru [9] constitute a loop which will replace every but the last newline character with a blank of the Pattern Space.