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))
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., 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