How to split a single Text TXT file by a string delimiter in Node.JS

Today I’m going to show you how to split apart a TXT file with a string delimiter. Sounds daunting, doesn’t it? Well don’t worry, we have a trump card up our sleeve. Instead of doing all the hard work normally required, we are going to employ a handy-dandy API to make our lives super easy.

To use our API, we will first need to install its client with this command.

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

Now just call our function splitDocumentTxtByString and enter your file and delimiter.

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 splitDelimiter = "splitDelimiter_example"; // String | Required; String to split up the input file onvar opts = {'skipEmptyElements': true // Boolean | Optional; If true, empty elements will be skipped in the output};var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.splitDocumentTxtByString(inputFile, splitDelimiter, opts, callback);

That’s it! Already done. Imagine how annoying that would have been the old fashioned way!

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