Description

The desired output file should satisfy the following requirements:

  • It consists of 1-line records.
  • Each record begins with 'RECORD', followd by one or more spaces, then one or more fields separated by spaces.
Due to some typing errors, some records are split to multiple lines, what we want to do is reformatting the file to satisfy the requirements. The data file in 'wrong' and 'desired' formats are shown as follows:

Raw Input
RECORD data1.0 data1.1
data1.2
data1.3 data1.4
RECORD data2.0
data2.1 data2.2
data2.3
RECORD
data3.0 data3.1
data3.2
Desired Output
RECORD data1.0 data1.1 data1.2 data1.3 data1.4
RECORD data2.0 data2.1 data2.2 data2.3
RECORD data3.0 data3.1 data3.2
Script and Comments
Script1
[ 1] :loop
[ 2] N
[ 3] /\nRECORD/!{
[ 4] s/\n/ /
[ 5] b loop
[ 6] }
[ 7] P
[ 8] D
Comments
  1. 'Pattern Space' will be abbreviated to 'PS'.
  2. Steps [1] through [6] constitute a loop.
  3. Step [2] will join next line to PS.
  4. If the line joined in Step [2] does not begin with 'RECORD', step [3] will replace the newline character with space, then go to step [1].
  5. Otherwise, it implies that PS has two lines beginning with 'RECORD', we print and then delete the first line of PS, then go to step [1].