split-by-line
A NodeJS module that splits a file into parts while preserving lines.
Install with npm install split-by-line
Example
Split by line splits a file into a specified number of files but preserves the lines of the file.
const splitByLine = require('split-by-line'); splitByLine('file.txt', { number: 3}, function(err, files) { // files is an array with the resulting filenames console.log(files); // Prints: // ['file.txt.part0', 'file.txt.part1', 'file.txt.part2']});
Api
splitByLine(inputFile, [opts], callback)
- the first parameter is the file that gets split
- the second parameter are optional options
- the callback will be called with
err
andfiles
, where files is an array containing the file names of the created files
Currently it is not guaranteed that the lines stay in order
Options
options = { number: 2, // The number of files that are created (1000 is maximum) keepEmpty: true, // If the number of files is bigger than the number of lines, decide whether to keep the remaining empty files or not outputDir: path.dirname(inputFile), // The folder in which the files are stored minLines: 1 // The minimum number of lines one file should have.}