1

hello i have a txt problem but when i deleted the line i have a space i want to remove the line spacing

my txt is like this

I tried to erase it in regex in several ways none works I tried with his [\ r \ n] +. and its ^ (?: [\ t] * (?: \ r? \ n | \ r)) +

and its \ s + (?! [^,])

is there the possibility of simulating a key down arrow and a backspace or if you have a better solution I am a taker ->first line abc abc

 fs = require('fs');
    data = fs.readFileSync('list.txt', 'UTF-8');
    lines = data.split('\r\n');
      for(var i = 0; i < lines.length; i++){
        var newValue = data.replace(lines[i] , '');
        
        fs.writeFileSync('list.txt' , newValue   );

x_zone web
  • 25
  • 4

1 Answers1

0

You're setting newValue each time in the for loop, meaning that your output is just going to be the last line with the given code.

Perhaps something a little more simple would work for you, if you're just trying to replace the newlines.

 fs = require('fs');
 data = fs.readFileSync('list.txt', 'UTF-8');
 lines = data.split('\r\n').join('');
 fs.writeFileSync('list.txt' , lines);

Or even more simply put:

 fs = require('fs');
 data = fs.readFileSync('list.txt', 'UTF-8');
 // note the "g" for "global" in the RegExp
 fs.writeFileSync('list.txt' , data.replace(/\r\n/g,""));
Cody G
  • 8,368
  • 2
  • 35
  • 50
  • this work but i want removing blank line data.replace(/\r\n/g,"")) this or this data.replace(lines[i] , ''); replace lines but i got the first line but i got line blank and i have try many thing for removing and nothing if you have an idea about this if i can simulate keypress down arrow and backspace to remove the first blank line or a trick like regex thanks you in avance for your help – x_zone web May 01 '21 at 03:05
  • `"\r\n\r\ntest\r\n".replace(/\r\n/g,"")` replaces even if there is a first blank line. So perhaps your first blank line is just `\r` or `\n` ? What number do you get if you do `data.charCodeAt(0)`? and `(1)` ? (e.g. `"A".charCodeAt(0)` returns `65` ) – Cody G May 01 '21 at 05:40
  • thanks you i have solved my problem by this way var newValue = data.replace(lines[i] , ''); fs.writeFileSync('list.txt' , newValue ); fs.writeFileSync('list.txt' , data.replace(/^(?:[\t ]*(?:\r?\n|\r))+/g ,"") ); – x_zone web May 01 '21 at 12:03
  • excuse me sir i have a other question i have set up process on handled error on my script but on handled error if script die it run again but i want to set up one thing if the script got like 10 error the processs exit the script i have try like this but i don't know if it's good process.on('unhandledRejection', (reason, p) => { console.error('Unhandled Rejection at: Promise', p, 'reason:', reason); run(); }); if(process.on('unhandledRejection') == 10 { process.exit(); } – x_zone web May 01 '21 at 12:12
  • The `unhandledRejection` event handler should really be used as a *final* process exit. Use `try` and `catch` in your program instead. – Cody G May 01 '21 at 14:54
  • thank you for your precious help its helped me a lot and for the rejection handler here I need it to be realized in this way that when it realizes 10 error it quits the procedure I did something but I do not know if it is is correct i have not yet come across the error case to see it i could see it that in the practical case of my script is that you can take a look at this and tell me if this is correct – x_zone web May 01 '21 at 22:59
  • process.on ('unhandledRejection', (reason, p) => { console.error ('Unhandled Rejection at: Promise', p, 'reason:', reason); run (); }); if (process.on ('unhandledRejection', == 10, (reason, p) => { process.exit (); } – x_zone web May 01 '21 at 22:59
  • hello sorry for bothoring you can you help me with this query ? – x_zone web May 02 '21 at 23:50
  • https://stackoverflow.com/questions/67361911/i-would-like-to-create-an-unhandledrejection-if-it-returns-several-errors-afterw – x_zone web May 02 '21 at 23:50