×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

To divide a file in other files!

To divide a file in other files!

To divide a file in other files!

(OP)
I want to divide a file in other files but not in just one file but in many files. Like a division of file in many file.

RE: To divide a file in other files!

And the question is?.... ;^)

A little more info please. For example, what do your original files look like, and how do you want to divide them?

Thanks,

Andy

RE: To divide a file in other files!

(OP)
ok! The files is a data file with a caractere and line feed et cariache return! I want to split a file in many file with to take 66lines with the original files and make a other one. I do that a each 66line of data. Im not a beginner in programmation but i start in Perl. I need help to learn how i cant do that. P.S. Im in DOS operation.

RE: To divide a file in other files!

OK, I've got an idea on a quick and dirty script that should do what you need, but I don't want to post it yet until I've had a quick chance to try it myself. (There's nothing worse than feeding a beginner bad information :^)

I'll have a look at something later, and try to get it posted tomorrow. That way you should get to see it before the world ends at midnight on New Year's Eve ;^)

Bear with me, and we should be able to sort something out. :)

RE: To divide a file in other files!

Heck, got bored waiting for a backup to finish, so here's the script.

WARNING: It's been written and tested using perl V4.036 (I know, I know, but I can't upgrade to v5 on the box I was using) under Unix. Things like file names, etc, will need changing. Also, it worked on the quick and dirty test that I did, but you might need to amend things a little before you use it in a production environment.

Script begins below:

#!/usr/gnu/bin/perl

# The above line will need changing to point to your local perl binary. If
# running under DOS, run the script through pl2bat to generate a batch file.

# Divide an input file into an arbitray number of files as specified by a number
# of lines.

$Input_File = "testfile"; # Name of the input file.
$Output_File_Prefix = "output"; # Name of all output files
# will start with this.

$Number_Of_Lines = 6; # Number of lines to split the file at.

# Setup some counters.
$Output_File_Counter = "0"; # Will be incremented for each file.
$Input_Line_Counter = "0"; # Will be incremented as each line is read in.

# Open the input file for reading.
open(INPUT_FILE, "<$Input_File");

# Now, loop around each line of the input file. As each line is read in, place
# it into an associative array (a hash) which is keyed on line number. Also,
# each line is read into a variable, so that we don't suffer from any $_ side
# effects.
while ($Current_Line = <INPUT_FILE>) {
# Increment line number counter at the beginning of the loop.
$Input_Line_Counter++;

# Place the current line into the hash.
$Output_Hash{$Input_Line_Counter} = $Current_Line;

# Have we reached our number of lines counter?
if ( $Input_Line_Counter == $Number_Of_Lines ) {
# We have.
# Open the output file for writing. This will overwrite any
# existing files of the same name.
open(OUTPUT_FILE, ">${Output_File_Prefix}.${Output_File_Counter}");

# Output the lines. What this little bit of code does is
# read the keys of %Output_Hash, and sorts the keys into order.
# As the keys are entered as 1,2,3, ... then the lines should be read
# out in the same order as they were read in.
foreach $Key (sort keys %Output_Hash) {
print OUTPUT_FILE $Output_Hash{$Key};
}

# Close the output file.
close(OUTPUT_FILE);

# Increase the file counter.
$Output_File_Counter++;

# Reset the line number counter to 0.
$Input_Line_Counter = 0;
}
}

close(INPUT_FILE);

Script ends above.

Comments in the file should, hopefully, explain what's going on.

You should be able to copy & paste it to test. Sorry if the formatting's gone a little out of whack during my posting. (Note to self: Must learn how to put formatting codes into Tek-Tips posts.)

Hope this helps.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close