Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Splitting a file into pieces

Posted by enquirer (enquirer), 18 September 2002
The following question was asked by a person who has a really long file of data - an NC tool program.

"I want to parse a file ... spliting out sections that are marked between a start and an end marker I have inserted into separate files.  How is it best to do this in Tcl"

Posted by admin (Graham Ellis), 19 September 2002
So let's say our incoming file looks like
Code:
This is a test piece of main data. It
contains some asides START This is an
aside which I want to put into the sub
file, by the way. END which I want to be
able to strip out. START There can be
several asides in one file END You
might use something like this to pull
a number of subroutines out of a file.


and we want two output files

Code:
This is a test piece of main data. It
contains some asides
which I want to be
able to strip out.
You
might use something like this to pull
a number of subroutines out of a file.


and

Code:
This is an
aside which I want to put into the sub
file, by the way.
There can be
several asides in one file


Then we could use a program like

Code:
# parse one file into two ....

# Sample code uses fixed file names demoinfile and demooutfile

set fhin [open demoinfile]
set fhout_main [open demooutfile w]
set fhout_sub [open demosubfile w]
set insub 0

while {[gets $fhin line] >= 0} {
       # loop through lines of incoming file
       # Write to one output file or the other, or a bit to both
       if {[regexp -- "(.*?)START(.*)\$" $line all pre post] > 0} {
               # switching into the sub file
               puts $fhout_main $pre
               puts $fhout_sub $post
               set insub 1
       } elseif {[regexp -- "(.*?)END(.*)\$" $line all pre post] > 0} {
               # switching from the sub file
               puts $fhout_main $post
               puts $fhout_sub $pre
               set insub 0
       } elseif {$insub == 1} {
               puts $fhout_sub $line
       } else {
               puts $fhout_main $line
       }
}


in which we're basically openeing out two output files and then spraying the results to one or the other.

Note that this simple "starter" code has some limitations - it will only handle one START or END per line, it gets into trouble if they're not nested correctly - but it's a starter to set you off in the write direction.

Posted by admin (Graham Ellis), 1 October 2002
Just a footnote - I really should have closed my files after I finished with them; not usually necessaryto make the program work since buffers are flushed and a programn completed on exit, but with embedded applications you can't be 100% certain that the Tcl interpretter is going to be shut down properly



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho