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
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
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., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho