How to Split a Single Text File by a String Delimiter using Node.js

Cloudmersive
2 min readDec 6, 2023

--

Text files use line numbers and string delimiters as breakpoints between text values. With the latter, we can divide up our text data extremely specific ways.

Using the code provided below, we can take advantage of a free API that lets us split a text file into multiple separate files based on a specific string delimiter. We could, for example, divide up a CSV file using commas as our string delimiter to return each individual data point on its own.

We can structure our API call in a few quick steps. First, we can install the SDK by either running the below command:

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

Or by adding the following snippet to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

Finally, we can copy the below code into our file, and we can enter a free-tier API key to authorize our request (this allows a limit of 800 API calls per month with no commitment):

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.SplitDocumentApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.

var splitDelimiter = "splitDelimiter_example"; // String | Required; String to split up the input file on

var 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);

Now we can quickly and easily manipulate text data with custom string delimiter values.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet