Description
Given a data file,
  • a KEYWORD record is followed by a DESC record.
  • a KEYWORD/DESC record may consists of several lines, in this case, we want to join these lines to one.
  • a DESC record is followed by a blank line or end of the file.
Raw Input
BOOK: Sed & Awk
KEYWORD: sed,
awk, script language,
regular expressions
DESC: good book
on string processing
with regular expressions.
BOOK: Mastering Regular Expressions KEYWORD: perl, script language, regular expressions DESC: a concrete book on string processing, several languages are introduced.
Desired Output
BOOK: Sed & Awk
KEYWORD: sed, awk, script language, regular expressions
DESC: good book on string processing with regular expressions.
BOOK: Mastering Regular Expressions KEYWORD: perl, script language, regular expressions DESC: a concrete book on string processing, several languages are introduced.
Script and Comments
Script1
[ 1] /^KEYWORD/!b
[ 2] :loop0
[ 3] N
[ 4] /\nDESC/!b loop0
[ 5] h
[ 6] s/\n[^\n]*$//
[ 7] s/\n/ /g
[ 8] p
[ 9] G
[10] s/^.*\n//
[11] :loop1
[12] $!{
[13] N
[14] /\n$/!b loop1
[15] }
[16] s/\n\(.\)/ \1/g
Comments
  1. Pattern and Hold Space are abbreviated to PS and HS, respectively.
  2. Whenever a 'KEYWORD' line is read, the loop consists of Step [2] thru [4] will join all lines until one beginning with 'DESC' is found.
  3. Now, PS contains a whole 'KEYWORD' record and the first line of a 'DESC' record.:
    • Step [5] is used to copy the contents of PS to HS.
    • Step [6] will trim off the 'DESC' record.
    • Step [7] will replace every newline character with a space. After this step, PS will contain the one-line 'KEYWORD' record, and Step [8] will print it.
  4. Step [9] will copy the contents of HS to PS.
  5. After Step [10], PS will contain the first line of a 'DESC' record.
  6. The loop consists of Step [11] thru [15] will join all lines of a 'DESC' record.
  7. Step [16] will replace every but the last trailing newline character with a space.