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.

Writting a script to break a file

Posted by saleem (saleem), 3 June 2005
Hi

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:

line1
line2
line3
line4
line5
.......

I want to split this way:
file1_1:
line1
line2

file1_2:
line3
line4

and so on.

any help please.

regards

---Sal

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:

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


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:

/stage/test/usr/data1/Light_1.root
/stage/test/usr/data1/Light_2.root
/stage/test/usr/data1/Light_3.root
/stage/test/usr/data1/Light_4.root
/stage/test/usr/data1/Light_5.root
/stage/test/usr/data1/Light_6.root

I want to put:
/stage/test/usr/data1/Light_1.root
/stage/test/usr/data1/Light_2.root

in file1_1

/stage/test/usr/data1/Light_3.root
/stage/test/usr/data1/Light_4.root

in file1_2

and so on.

Thanks for your time.

regards

---sal




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

($n++)%2

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 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