Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Mr.Stork

Need to Split 1k Lines Text File into 10 separate text files (every 100 lines)

Recommended Posts

Posted · Original PosterOP

Looking For a Software or Website that makes a new text file from every 100 lines from a large text file which as 1000lines.

 

Link to post
Share on other sites

Gsplit has options like that : https://www.gdgsoft.com/gsplit

 

Split file after the "new line" character was found 100 times. Something like that.

 

It can be done in php in a few lines but that software can do it easily. 

The php could would be something like this ... try it, though it may not work due to some typo, as I'm writing it directly in this message box :

basically php.exe script.php  with your text file as input.txt in same folder as the script.php file. You can change the number of lines by changing the 100 value.

<?php

// read input.txt in memory (input.txt must be in same folder where .php file is located)
$data = file_get_contents(__DIR__ .'/input.txt');
// split it into individual lines into an array, split on enter sequence 0x0D 0x0A 
$lines = explode(chr(0x0D).chr(0x0A),$data);
// which output file we are working with
$counter = 1;
// tracks the output file opened
$handle = fopen( __DIR__ .'/output.'.$counter.'.txt','wb');
// how many lines were written in current output file
$lineswritten = 0;
foreach ($lines as $line) {
  // add ENTER characters before writing new line to an output text file
  // but don't do it if you're at the start of an output file (or you get a blank line)
  if ($lineswritten!=0) fwrite($handle, chr(0x0D).chr(0x0A)); 
  // write the line to file
  fwrite($handle,$line);
  $lineswritten = $lineswritten+1;
  // did we write 100 lines? Close file, increase counter and open next output file
  if ($lineswritten == 100) {
    fclose($handle);
    $counter = $counter+1;
    $handle = fopen( __DIR__ .'/output.'.$counter.'.txt','wb');
    $lineswritten = 0;
  }
}
fclose($handle);
echo 'Done.';
?>

 

 

// and if you don't care about cutting at end of a line, you can use the "Split file..." option in Total Commander, which is like a Windows Explorer but much much better. You can split after a number of bytes , KB, MB etc 

Link to post
Share on other sites
<?php
$ff=file("bigfile.txt");
$ffc=count($ff);
$n=99;
$r=0;
$op=0;
for($t=0;$t<$ffc;$t++){
$n++;
if ($n>99){
$r++;
if ($op==1) fclose($qq);
$qq=fopen("smallfile".$r.".txt","w");
$op=1;
$n=0;
}
fwrite($qq,$ff[$t]);
}
fclose($qq);
?>

Less elegant code, but smaller. :)

 

Link to post
Share on other sites

Going for small php code, 4 lines

$contents = file('bigfile.txt'); // read each line of file into array
$chunks = array_chunk($contents, 100); // split array, into 100 chunks
foreach($chunks as $i => $chunk)
    file_put_contents("output2/smallfile-{$i}.txt", $chunk); // write chunk to file

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Newegg

×