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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho