Created
March 3, 2016 01:09
-
-
Save jamesmontalvo3/e6684b2a67c28276ffc1 to your computer and use it in GitHub Desktop.
Split a file every 20000 lines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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