Skip to content

Instantly share code, notes, and snippets.

@jamesmontalvo3
Created March 3, 2016 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmontalvo3/e6684b2a67c28276ffc1 to your computer and use it in GitHub Desktop.
Save jamesmontalvo3/e6684b2a67c28276ffc1 to your computer and use it in GitHub Desktop.
Split a file every 20000 lines
fs = require('fs');
path = require('path');
var log = path.join( path.dirname( fs.realpathSync(__filename) ), 'meza-dev.log' );
fs.readFile( log, 'utf8', function(err,data){
if (err) {
return console.log(err);
}
var lines = data.split("\n");
var count = lines.length;
console.log( count + " lines..." );
var i = 0;
var newFileText = "";
while( lines[i] ) {
newFileText += lines[i] + "\n";
if ( i % 20000 === 0 ) {
fs.writeFile( "log" + i + ".log", newFileText.substr(), 'utf8', function(err){
if(err) { console.log(err); }
});
newFileText = "";
}
i++;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment