Home > database >  Write to particular part of text file NodeJS
Write to particular part of text file NodeJS

Time:11-06

I have a text file like this:

lorem ipsum

some text

even more lorem ipsum

How do I select that "some text" bit of the file and and write to it eg. "more lorem ipsum".
Note: The "some text" section will not have a constant value.
Thanks in advance!

CodePudding user response:

Using line-replace npm module:

const lineReplace = require('line-replace')
lineReplace({
  file: 'yourtextfilename.txt',
  line: 3, // line number starts from 1 
  text: 'more lorem ipsum',
  addNewLine: true,
  callback: ({ file, line, text, replacedText, error }) => {}
})

PS: This question has been already answered multiple times before, please do your own research before posting a question.

  • Related