最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论