Find line in txt file, delete whole line

I am currently working with node.js to create an IRC bot. The bot allows users to add links to links in the database. Each time someone submits a song, it is added to the new line "shuffle.txt" as such:

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

Please note that user1 did not accept any information in his last addition. I am trying to make a UNDO command so that the user can delete their last entered line. I plan to do this by finding the last occurrence of my name in shuffle.txt and deleting the entire string found. Here is my message listener:

 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 } } }); 

the user entering the command is saved as from

I assume that I need to do something according to this:

 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); } } 

I am new to JavaScript and this is my first time using node.js so I can use any help I can get! Thanks to everyone.

EDIT: I should also point out that I do not need help in recognizing commands. I need help only with the search / delete part. Hooray!

+6
source share
2 answers

Edit2: edited with a new solution.

You can try the following:

 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]; }); 

Divide the file into lines, find the last line with a simple loop going backward, delete the line with delete , then copy it together.

demo

In addition, you should not use readFileSync in node, as blocking the node can be dangerous because it is single-threaded.

+2
source

Everything seems to be working fine. It itself is contained, so you can simply paste it into an HTML file to see it run. Basically, continue to use the regex to match the entire line starting with the missing username. The entire string is returned (the "gm" part of the regular expression indicates that it matches the string at a time) as a string.

Then you just replace that returned row (like a row) with data. Note that this assumes the string is unique in the text file. If you think that people could enter one line several times (but just want to delete the last one), then the β€œnot lazy” option, which I left without comment, is best. I did not check what would happen if the row is the first in the data or the last.

 <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> 

hope this helps!

+1
source

All Articles