在Txt文件中查找string,删除整行

我目前正在使用node.js创build一个IRC bot。 bot允许用户将歌曲链接添加到数据库。 每次有人提交歌曲时,都会将其添加到“shuffle.txt”的新行中,如下所示:

user1,The Beatles,Yesterday,(youtube link) user2,The Rolling Stones,Angie,(youtube link) user1,The Bealtes,Yellow Sumbarine,(youtube link) 

请注意,user1错误地input了最新的信息。 我试图做一个UNDO命令,以便用户可以删除他们最近input的行。 我打算通过在shuffle.txt中查找它们的名称的最新发生并删除它发现的整个行来做到这一点。 这是我的消息监听器:

 bot.addListener('message', function(from, to, message) { if (message.indexOf(config.prefix) == 0) { message = message.slice(1); var token = message.split(" "); if (token[0] == 'undo') { //find and delete } } }); 

input命令的用户存储为from

我假设我将不得不按照这样的方式做一些事情:

 var songList = fs.readFileSync('shuffle.txt', 'utf8'); var position = songList.indexOf(from); if (position != -1) { //if 'from' found //find LAST occurrence of 'from' //get length from here to next occurrence of '\n' //substr(length + 1) fs.writeFile('shuffle.txt', songList, function(err) { if (err) { console.log (err); } } 

我是JavaScript新手,这是我第一次使用node.js,所以我可以使用任何帮助,我可以得到! 感谢大家。

编辑:我也应该指出,我不需要帮助与命令识别。 我只需要find/删除部分的帮助。 干杯!

编辑2:编辑一个新的解决scheme。

你可以试试这个:

 fs.readFile('shuffle.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('user1')) return i; }() delete data_array[lastIndex]; }); 

将文件拆分成行,用简单的循环find最后一行,使用delete行,然后将它重新拼回去。

演示

此外,您不应该在节点中使用readFileSync ,因为阻塞节点是单线程的,所以它可能很危险。

这似乎工作正常。 它是自包含的,所以你可以粘贴到一个HTML文件来看它运行。 基本上继续运行一个正则expression式来匹配从传递的用户名开始的整个行。 整行被返回(正则expression式的“gm”部分告诉它一次匹配一行)作为一个string。

然后你只需要在数据中replace返回的行(作为string)。 请注意,这假定该行在文本文件中是唯一的。 如果你认为人们可能已经进入了同一行几次(但只想删除最后一行),那么我留下未注释的“非懒惰”选项是最好的。 我还没有testing过,如果行是第一个数据或最后一个会发生什么。

 <html> <head> <script type="text/javascript"> var sData="user1,The Beatles,Yesterday,(youtube link)\nuser2,The Rolling Stones,Angie,(youtube link)\nuser1,The Bealtes,Yellow Sumbarine,(youtube link)\nuser3,The Rolling Stones,Angie,(youtube link)"; function replaceLastEntry(sText) { sNewData=sData; var re=new RegExp("^"+sText+".*$","gm"); // matches whole lines starting with sText var lastIndex=-1; var i=0; var sMatch=re.exec(sData)!= null; while (sMatch!=null) { i++ lastIndex=re.lastIndex; lastMatch=sMatch.toString(); // make note of last successful match - gives full line sMatch=re.exec(sData); } // at this point lastMatch contains the whole line which needs to be removed. // lastIndex is the string position of the character after the LAST string matched (ie the end of the matched line) // sNewData = sData.replace(lastMatch,""); // Lazy way - assumes the line is unique within the file // non-lazy way : locate the text by position returned and remove it sNewData = sData.substr(0, lastIndex-lastMatch.length-1) + sData.substr(lastIndex); document.getElementById("Data").innerHTML=sData document.getElementById("NewData").innerHTML=sNewData document.getElementById("LastMatch").innerHTML=lastMatch } </script> </head> <body onload="replaceLastEntry('user1','newvalue')"> Data: <pre id="Data"></pre> New Data:<pre id="NewData"></pre> Last Match: <pre id="LastMatch"></pre> </body> </html> 

希望这可以帮助 !