Created
May 21, 2021 02:22
-
-
Save afshinator/15ba0cbe1780aa84cb71145eebc468a3 to your computer and use it in GitHub Desktop.
A little text file parser to help the good Doctor Schevik out.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require('readline'); // https://nodejs.org/api/readline.html | |
const fs = require('fs'); // The stream for readline will be from a file | |
// Create the object that will read from the stream | |
const readInterface = readline.createInterface({ | |
input: fs.createReadStream('./data2.txt'), // 🧐 Data filename goes here | |
// output: process.stdout, | |
console: false | |
}); | |
let linesCount = 0, recordsProcessed = 0, nameIndex, controlIndex | |
let snippet1, snippet2, fname, lname, controlNum, date, paymentSum | |
// Read a line at a time, do something with it | |
readInterface.on('line', function (line) { | |
linesCount++ | |
if (line.length > 1) { // Only look at non-empty lines | |
// 'first line' in a record should have patient name and control # | |
if ((nameIndex = line.indexOf('Patient:')) !== -1 && (controlIndex = line.indexOf('Control #:')) !== -1) { | |
snippet1 = line.substring(nameIndex + 9) | |
fname = snippet1.substring(0, snippet1.indexOf(' ')) | |
lname = snippet1.substring(snippet1.indexOf(' ') + 1) | |
lname = lname.substring(0, lname.indexOf('\t')) | |
snippet2 = line.substring(controlIndex + 11) | |
controlNum = snippet2.substring(0, snippet2.indexOf('\t')) | |
} | |
// Can have misc lines of transactions; lets grab the date off the first one | |
if (line.indexOf('0100') !== -1) { | |
date = line.substring(7, 13) | |
} | |
if (line.indexOf('Sub-Totals') !== -1) { | |
paymentSum = line.substring(line.lastIndexOf('\t') + 1) | |
recordsProcessed++ | |
if (fname.length + lname.length < 15) lname += ' ' | |
console.log(`${fname} ${lname}\tcontrol#: ${controlNum} \tdate:${date} \tsum:${paymentSum}`) | |
} | |
} | |
}); | |
readInterface.on('close', function(){ | |
console.log(`👍🏼Parsed ${recordsProcessed} records, in ${linesCount} lines.`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment