Version 2 of HOW TO - Splice lines of source code

Updated 2004-11-19 16:40:51

Given a file of source lines that look like the following, I would like to splice together lines with escaped new lines but preserve the line numbers.

Input File


 line(1)
 line(2)\
 line(3) with space after excape\    
 line(4)\
 line(5)
 line(6)\
 line(7)
 line(8)

Solution

 set fid [open [file normalize "./filename"] r]
 set text [read ${fid}]
 close $fid
 regsub -all -- {[\\][ \t]*[\n]} ${text} "\\\n" text
 while { [regsub -all -- {[\\][\n](.*?[^\\])[\n]} ${text} "\\1\n\n" text] } {}

Result

 >% puts $text
 >line(1)
 >line(2)line(3) with space after excapeline(4)line(5)
 >
 >
 >
 >line(6)line(7)
 >
 >line(8)
 >
 >%

Notice that the lines have been spliced together and the spliced lines occur on the corrent line in the output.

Tom Krehbiel