Larry Battle
Larry Battle

Reputation: 9178

Overwrite a line in a file using node.js

What's the best way to overwrite a line in a large (2MB+) text file using node.js?

My current method involves

Upvotes: 8

Views: 9001

Answers (3)

This isn't file size focoused solution, but Overwrites a line in a file using node.js. It may help other people that search engines redirect to this post, like me.

import *  as fs from 'fs'

const filename = process.argv[2]
const lineIndexToUpdate = parseInt(process.argv[3]) - 1
const textUpdate = process.argv[4]

function filterLine(indexToUpdate, dataString) {
  return dataString
      .split('\n')
      .map((val, index) => {
        if (index === indexToUpdate)
          return textUpdate
        else
          return val
       })
      .join('\n')

}

fs.readFile(filename, 'utf8', (err, data) => {
  if (err) throw err

  fs.writeFile(filename, filterLine(lineIndexToUpdate, data), (err, data) => {
    if (err) throw err
    console.log("Line removed")
  })
})

Script use exemple:
node update_line.js file 10 "te voglio benne"

Upvotes: 0

ccw1078
ccw1078

Reputation: 31

Maybe you can try the package replace-in-file

suppose we have a txt file as below

// file.txt
"line1"
"line2"
"line5"
"line6"
"line1"
"line2"
"line5"
"line6"

and we want to replace:

line1 -> line3
line2 -> line4

Then, we can do it like this:

const replace = require('replace-in-file');

const options = {
    files: "./file.txt",
    from: [/line1/g, /line2/g],
    to: ["line3", "line4"]
};

replace(options)
.then(result => {
    console.log("Replacement results: ",result);
})
.catch(error => {
    console.log(error);
});

the result as below:

// file.txt
"line3"
"line4"
"line5"
"line6"
"line3"
"line4"
"line5"
"line6"

More details please refer to its docs: https://www.npmjs.com/package/replace-in-file

Upvotes: 2

Gabriel Llamas
Gabriel Llamas

Reputation: 18427

First, you need to search where the line starts and where it ends. Next you need to use a function for replacing the line. I have the solution for the first part using one of my libraries: Node-BufferedReader.

var lineToReplace = "your_line_to_replace";
var startLineOffset = 0;
var endLineOffset = 0;

new BufferedReader ("your_file", { encoding: "utf8" })
    .on ("error", function (error){
        console.log (error);
    })
    .on ("line", function (line, byteOffset){
        startLineOffset = endLineOffset;
        endLineOffset = byteOffset - 1; //byteOffset is the offset of the NEXT byte. -1 if it's the end of the file, if that's the case, endLineOffset = <the file size>

        if (line === lineToReplace ){
            console.log ("start: " + startLineOffset + ", end: " + endLineOffset +
                    ", length: " + (endLineOffset - startLineOffset));
            this.interrupt (); //interrupts the reading and finishes
        }
    })
    .read ();

Upvotes: 4

Related Questions