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 - 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))
Writting a script to break a file

Posted by saleem (saleem), 3 June 2005

I have a long file, I want to split this file into several files.
Any one have some idea how this can be done:
this is what i want:

file 1:


I want to split this way:


and so on.

any help please.



Posted by admin (Graham Ellis), 4 June 2005
I'm not sure what criteria you use to decide to finish one file and start the next ... but you probably want something like:

open (FHI,"file1");
open (FHO,">file1_1");
$outcount = 1;
while (<FHI>) {
    if (condition to see if new file needed) {
           open (FHO,">file1_$outcount);

Posted by saleem (saleem), 5 June 2005

Thank you for your reply.

I have this much working already, the problem is I want to break this file which has more than 500 lines.
Each line is almost same except there is a number changing in them.

So my problem is i want to put each two lines from this file into a seperate file.
If i try to put a loop this always gets only one line and put into another file. but how can make this to use 2 lines. this is how my lines look like:


I want to put:

in file1_1


in file1_2

and so on.

Thanks for your time.



Posted by admin (Graham Ellis), 5 June 2005
Replace the text "condition to see if new file needed" in my original example with the condition that applies in your case - which is going to be something like


The words in my original were only an example - I couldn't write the complete code for you as you hadn't told me about the condition.   In any case, we're here to help with programming problems and not write complete solutions  

This page is a thread posted to the opentalk forum at 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., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho