Take the 2-minute tour ×

split a file based on size and append a first "string pattern" to the end of part1 from the next part and maintain a particular size all time.

share|improve this question

closed as not a real question by Oesor, derobert, Michael, jball, greedybuddha Jun 14 '13 at 17:16


What is the "particular string"? You said it will be in file_part2, but how does the script find it? –  Barmar Feb 2 '13 at 7:48
This is a string that's hard-coded into the script, a pattern that it looks for in the file, or a parameter to the script? It's already in the file, or it's something that the script adds to the file? –  Barmar Feb 2 '13 at 8:06
Its hard coded in the file, and needs to go to the end of the file if it is split. Basically the script should take the first available pattern if any after the split. In case if there are many similar strings after 500th line and before 1000th line it should split and append first available "var:value_var_v1" @ the end of split file. –  sharp Feb 2 '13 at 8:09
I still don't understand. You say "append a first particular string", which means the script adds it. But you also say "script should search if there are more particular string", which means that it's already in the file. I think it would help if you showed some sample data. Show what the original file would look like, and what you want the result to be like. –  Barmar Feb 2 '13 at 8:17
Sorry, Let me put this way. @ the end of each file that "string" should be there. –  sharp Feb 2 '13 at 8:30

1 Answer 1

I think this is pretty close to what you want.

my $part_string = 'var:value_var_v';
my $file_count = 1;
my $total_length = 0;
my $max_length = 40000;

open (my $input, '<', $input_filename);
open (my $output, '>', "${output_filename}_part_${file_count}");
while (my $line = <$input>) {
  print $output $line;
  $total_length += length($line);
  if ($total_length > $max_length) {
    print $output "$part_string$file_count\n";
    close $output;
    open ($output, '>', "${output_filename}_part_${file_count}");
    $total_length = 0;
if ($total_length > 0) {
  print $output "$part_string$file_count\n";
close $output;
share|improve this answer
Thanks, but how would I run this? I tried nothing happened. –  sharp Feb 2 '13 at 18:39
I changed the value $input_filename, to my test.txt it created _part_1 but nothing in it. –  sharp Feb 2 '13 at 18:46
When I enabled warnings, Global symbol "$full" requires explicit package name at split.pl line 9. Global symbol "$output_filename" requires explicit package name at split.pl line 10. Global symbol "$output_filename" requires explicit package name at split.pl line 18. Bareword "test" not allowed while "strict subs" in use at split.pl line 9. Execution of split.pl aborted due to compilation errors. I am using windows. –  sharp Feb 2 '13 at 18:52
This is just an excerpt from the program, you need to write the rest. I expect you to initialize the ifilename variables from the arguments or however you want. I don't have a variable $full, so I don't know what you did to cause that -- but the error usually means that you forgot to use my when initializing it. I assume you know how to program, and are just having trouble with this algorithm. –  Barmar Feb 2 '13 at 19:50
I can hardly understand what you're trying to do, I'm not going to write the whole thing for you. If you don't know perl that well, use another language that you know better. My script should help you with the general algorithm, which is the same in any language. –  Barmar Feb 2 '13 at 20:47
