How to split a single Text File TXT into Lines in Node.JS

Our goal in this article is simple, to demonstrate how to split a TXT file into multiple lines from within Node.JS. Before you go trying to set this up from scratch, you should know that we already have the solution finished and packed together in an API that you can use to quickly solve this problem. Let me show you how to use it.

We begin with this installation command for our client.

npm install cloudmersive-convert-api-client --save

Now call splitDocumentTxtByLine:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveConvertApiClient.SplitDocumentApi();var inputFile = "/path/to/file"; // File | Input file to perform the operation on.var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.splitDocumentTxtByLine(inputFile, callback);

After inputting a TXT file, you are actually all done. Your newly split text will be returned line by line in order by the API.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store