javascript - Node js - Remove string from text file - Stack Overflow

How to i can remove string from text file ?fs.readFile('.banlist.txt', function read(err, da

How to i can remove string from text file ?

fs.readFile('./banlist.txt', function read(err, data) {
                if (err) {
                    throw err;
                }
                lastIndex = function(){
                    for (var i = data_array.length - 1; i > -1; i--) 
                    if (data_array[i].match(ip))
                        return i;
                }()
                delete data_array[lastIndex];
            });

But console give me message: data_array is not defined. I want to remove ip adress line.

How to i can remove string from text file ?

fs.readFile('./banlist.txt', function read(err, data) {
                if (err) {
                    throw err;
                }
                lastIndex = function(){
                    for (var i = data_array.length - 1; i > -1; i--) 
                    if (data_array[i].match(ip))
                        return i;
                }()
                delete data_array[lastIndex];
            });

But console give me message: data_array is not defined. I want to remove ip adress line.

Share Improve this question asked Jun 25, 2018 at 23:51 Yavuz Selim ÖzmenYavuz Selim Özmen 372 silver badges9 bronze badges 5
  • Where do you define data_array? I don't see it in this code snippet. – Adam P Commented Jun 25, 2018 at 23:53
  • everything in a text file is a string. and also fs.readFile does not populate data as an array, secondly strings are immutables. The delete key word only works on the members of an object. data_array is not defined any where. Check the arguments in your function if they are correct – 0.sh Commented Jun 25, 2018 at 23:56
  • you probably need to re-write the whole thing without that string. – Liang Commented Jun 25, 2018 at 23:59
  • Read the file, modify the data, then rewrite the file? (if it's a reasonable size...) – Chris Commented Jun 26, 2018 at 0:23
  • If it is not too big: var newData = data.toString().split('\n').filter(val=>val!==ip).join('\n') and then write newData (string) back to the file. – Chris Commented Jun 26, 2018 at 0:25
Add a ment  | 

1 Answer 1

Reset to default 3

Your code seems overly plicated. The biggest problem is that data_array doesn't exist, and data isn't an array. The simplest solution (though synchronous, which might be slow if you're dealing with a large file) is below:

var data = fs.readFileSync('banlist.txt', 'utf-8');
var ip = "STRING_TO_REMOVE";

var newValue = data.replace(new RegEx(ip), '');
fs.writeFileSync('banlist.txt', newValue, 'utf-8');

This will remove the first occurrence of the specified string from anywhere in the file. This means that if you're searching for "foo" and your file contains "This is foobar." it will end up as "This is bar.". If you have items on separate lines and want to remove any items that match, please clarify that in your question.

The above was adapted from this answer.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745228083a4617554.html

相关推荐

  • javascript - Node js - Remove string from text file - Stack Overflow

    How to i can remove string from text file ?fs.readFile('.banlist.txt', function read(err, da

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信