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

Cloudmersive
1 min readApr 12, 2020

--

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.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet