Reputation: 1241
I'm trying to do loop to write files, I need to know how to load a file as cities.txt, this content inside looks like:
Los Angeles
San Francisco
St. Louis
New York
Philadelphia
Miami
Houston
Dallas
Kansas City
Memphis
...
This list includes new line, I'm looking a script to do loop, the output would be like this screenshot:
Also the space string needs to be replaced from " " to "-" to prevent error due to naming file rules.
Upvotes: 0
Views: 242
Reputation: 17755
You can do it with readline, the core node module.
var fs = require('fs');
var lr = require('readline').createInterface({
input: require('fs').createReadStream('cities.txt')
});
lr.on('line', function (line) {
var fileName = line.replace(" ", "-").toLowerCase();
fs.writeFileSync(fileName + ".json", '');
});
This SO thread is quite helpful: Read a file one line at a time in node.js?
Upvotes: 1