T
T
TheWWCode2022-02-10 13:53:24
Node.js
TheWWCode, 2022-02-10 13:53:24

How to get the first 10 lines from a text file and remove them from there?

I have a .txt file with lines:

123123
123123
aaa12123
asdasasd123...

I need to take N number of lines from the beginning, write them to a variable and immediately delete them from the file. I will be very grateful for help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2022-02-10
@TheWWCode

const fs = require('fs')

try {
  const data = fs.readFileSync('/Users/joe/test.txt', 'utf8')
  console.log(data)
} catch (err) {
  console.error(err)
}

further data splitting
https://developer.mozilla.org/en/docs/Web/JavaScri...
write the first 10 to yourself
, write the rest to another variable
, then join on the second variable
and write to the file

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question