1

I would like to truncate a file by newline \n so that it only grows to some max number of lines. How do I do that with something like fs.appendFileSync?

Lance
  • 75,200
  • 93
  • 289
  • 503
  • 1
    The operating system does not generally contain functions for removing bytes from the beginning of a file. Thus, there is no efficient way to do this without reading and copying everything except some lines at the beginning of the file. This would typically be done, not on a continual basis but by some "aging" or "cleanup" process that runs every once in awhile. – jfriend00 Oct 02 '19 at 09:19

1 Answers1

0

You can address this problem by investigating readline API from node:

const fs = require('fs');
const readline = require('readline');

async function processLineByLine() {
  const fileStream = fs.createReadStream('input.txt');

  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity
  });

  for await (const line of rl) {
    // count your lines in the file
    // you can copy into output stream the content
    // of every line till it did not pass the max line number
  }

  // if the counter is not yet finished using 
  // rl.write() you can continue appending to the file
}

processLineByLine();

A second idea very similar to this one was answered here: Parsing huge logfiles in Node.js - read in line-by-line

Alexandru Olaru
  • 6,842
  • 6
  • 27
  • 53
  • This does not appear to answer the question. The OP wants to add new lines to the file, but automatically truncate the file so that it only grows to some max number of lines. That's the operative part of the question. Your answer does not show how to do that at all. It's also not going to be very practical if you have to read the entire file to truncate it every time you add a few lines to the end of the file. Hint: this isn't actually a trivial problem to solve efficiently. – jfriend00 Oct 02 '19 at 09:11
  • For sure this is not a complete solution, cause we will miss the point of having a discussion. I would not agree though that this does not show at all. – Alexandru Olaru Oct 02 '19 at 09:34