Link to home
Start Free TrialLog in
Avatar of error2013
error2013

asked on

Node read data from file line by line ... process it and save it back issue

In a file I have data that looks like this:

id=1349 console.log('done this') status=waiting
id=2345 console.log('done this too') status=waiting

Open in new window


If will read a line at the time, then change status to complete and save it back to the file.

So this is what I've got:

fs.readFile('list.txt', 'utf8', function (err,data) {

  if (data.includes('status=waiting')) {

    const str = data.trim();
    const arr = str.replace(/;$/gm, '').split(/\n/g)

    for (var i = 0; i < arr.length; i++) {

      var result = arr[i].replace('status=waiting', 'status=complete');

      var temp = result.split(" ");
      console.log(temp[1]);
      eval(temp[1]); //run the console.log

      console.log(`RESULT IS: ${result}`); // This line should now have status=complete when it saves below

      // Now Save it back with the status=complete in it
      fs.writeFile('list.txt', result, 'utf8', function (err) {
        if (err) return console.log(err);
      });
      

    };


  }

});

Open in new window


The problem is the saving... Although variable result is correct before saving, it's just replacing the file text and only saving the last one.

How can I fix this?

ps: I know eval shouldn't be used but this is just for personal use.
ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Likely this is a small file, so here's how I'd write the code... if you're working with small files...

1) Disable interrupts.

2) Read all lines of file.

3) Close file + reopen file for writing, to set seek pointer back to byte 0.

4) Process + write all lines.

5) Close file to flush buffers (or at least schedule buffers for flushing by OS).

6) Enable interrupts.

7) Do any other processing required + exit.
You are dealing with variable length lines in a file.

What you have to do is
a) Read in the entire file
b) Split the file up by line break so that each line is in a new element of an array
c) Find and update the array element you want to change
d) Join the array items back again and write to the file.

Because your file is unstructured you can't just read a line and write it back - you have to read and write the entire file.
Avatar of error2013
error2013

ASKER

m
You said...

I've requested that this question be deleted for the following reason:

I wanted some code.

This is a poor reason for requesting a deletion. You're basically asking someone else to do your work for you.

Sometimes code is provided. Sometimes simple direction is provided, with coding left to you to do yourself or hire out.

This question contains good conceptional information, regards overwriting file data in place.

Likely best for this question to be kept on file, as others will find value in the answers.
I disagree with what you said about the work being done for me as the code was already done and the issue was that something on it was not working.

Also, this is not stack overflow, I pay for asking these questions.

ps: I not longer need this solved as I've solved it already and also I thought I accepted your solution and closed the question but apparently it's not happened
Refer to my comments.

It's true you've written code + the code you've written simply requires some slight modification.

I provided 2x possible solutions.

There are also other solution.

Bottom line is both reading + writing to same file means you have 2x cursors pointing into the same file, which will always fail in subtle ways... also fail in different ways... at different times, depending on OS buffer flushing + filesystem garbage collection.

You said, "I've solved it already". Be great is you can mention your solution so someone reading this in the future can make use of the solution.
I actually marked one of yours as a solution.