Find string and delete line - Node.JS
var fs = require('fs')
fs.readFile('shuffle.txt', {encoding: 'utf-8'}, function(err, data) {
if (err) throw error;
let dataArray = data.split('\n'); // convert file data in an array
const searchKeyword = 'user1'; // we are looking for a line, contains, key word 'user1' in the file
let lastIndex = -1; // let say, we have not found the keyword
for (let index=0; index<dataArray.length; index++) {
if (dataArray[index].includes(searchKeyword)) { // check if a line contains the 'user1' keyword
lastIndex = index; // found a line includes a 'user1' keyword
break;
}
}
dataArray.splice(lastIndex, 1); // remove the keyword 'user1' from the data Array
// UPDATE FILE WITH NEW DATA
// IN CASE YOU WANT TO UPDATE THE CONTENT IN YOUR FILE
// THIS WILL REMOVE THE LINE CONTAINS 'user1' IN YOUR shuffle.txt FILE
const updatedData = dataArray.join('\n');
fs.writeFile('shuffle.txt', updatedData, (err) => {
if (err) throw err;
console.log ('Successfully updated the file data');
});
});
This link explains how to find a string and delete lines but only delete one user1 at the time. I have many lines with user1, how can I delete all:
john
doe
some keyword
user1
last word
user1
user1
Also the opposite. How can I delete all the lines and leave only the user1 lines?
I would make use of the Array.filter() function.
Basically, you call filter() on an array, and define a callback function that checks every item of that array.
If the checking function returns true for a particular item, keep that item - put it in a new array
If the checking function returns false, do not put the item in the new array
So, in your case, once you have read all the lines into an array (up to line 6 in your code), use the filter function:
// Delete all instances of user1
let newDataArray = dataArray.filter(line => line !== "user1")
// Delete everything except user1
let newDataArray = dataArray.filter(line => line === "user1")
// Delete any lines that have the text 'user1' somewhere inside them
let newDataArray = dataArray.filter(line => !line.includes("user1"))
Then, just like you have done in your code, use the join() function on newDataArray() and write the file.
To rewrite your code,
var fs = require('fs')
fs.readFile('shuffle.txt', {encoding: 'utf-8'}, function(err, data) {
if (err) throw error;
let dataArray = data.split('\n'); // convert file data in an array
const searchKeyword = 'user1'; // we are looking for a line, contains, key word 'user1' in the file
// Delete all instances of user1
let newDataArray = dataArray.filter(line => line !== searchKeyword)
// UPDATE FILE WITH NEW DATA
const updatedData = newDataArray.join('\n');
fs.writeFile('shuffle.txt', updatedData, (err) => {
if (err) throw err;
console.log ('Successfully updated the file data');
});
});
Related
In my case I got the data from firestore now how can I save it to
serversettings.json:
var temp = {}
let query = db.collection('guilds')
let data = query.get().then(snapshot => {
snapshot.forEach(doc => {
console.log(doc.id, '=>',doc.data());
})
so I get the output as:
637301291068030997 => { welcomeChannelID: '<#648968505160630285>',
guildMemberCount: 4,
guildOwnerID: '348832732647784460',
guildOwner: 'Ethical Hacker',
prefix: '.',
guildID: '637301291068030997',
guildName: 'test server 3' }
and this:
GUqGqFanJuN7cRJx4S2w => {}
I need to save that data to serversettings.json
await fs.writeFile ("../serversettings.json", JSON.stringify(temp), function(err) {
if (err) throw err;
console.log('done');
})
here temp is variable where multiple data is stored like a:{},b:{}....
i tried var temp = {} temp.table = [] and then temp.table.push(doc.id, ':',doc.data())
but i get empty output so what can i do to get that expected output ?
also, adding to that how can I update the values if that object is already present in JSON will the above function work the same will it override the same value or delete all other values example update prefix from. to, then await fs.writeFile ("../serversettings.json", JSON.stringify(temp),..... so the temp field has a value of only guild id and that field prefix will it update the only prefix and not delete anything else in that array?
HERE is the code that added stuff to temp variable
var temp = {}
temp.guilds = [] // after some lines
snapshot.forEach(doc => {
console.log(doc.id, '=>',doc.data()); // output is above this code
temp.guilds.push(doc.id = doc.data()) // output is below this code
})
Above codes output
{ guilds:
[ { guildID: '637301291068030997', // here missing doc.id field
guildName: 'test server 3',
welcomeChannelID: '-',
guildMemberCount: 4,
guildOwnerID: '348832732647784460',
guildOwner: 'Ethical Hacker',
prefix: '.' },
{} // this missing thing before {} is (some number) also bracket is empty by the way so no worries
]
}
A fast solution for your issue would be to replace
let data = query.get().then(snapshot => {
with
await query.get().then(snapshot => {
so that your temp object can be filled before the program proceeds to save the file.
I haven't used writeFile yet, but here's what its documentation says:
When file is a filename, asynchronously writes data to the file, replacing the file if it already exists.
I don't think your object will be so large that a complete overwrite would be a problem, unless it's changing very often. In that case, I guess you'd have to use a different method that can support an offset, so that you can write only what has changed, but that seems like a real overkill.
Regarding the format of your JSON file, I think what you're trying to do is this:
var temp = {};
temp.guilds = {};
snapshot.forEach(doc => {
console.log(doc.id, '=>', doc.data());
temp.guilds[doc.id] = doc.data();
});
i`m not sure , but firebase has method to convert data from object to JSON, so i think this solution should work
let query = db.collection('guilds')
let data = query.get().then(snapshot => {
let temp = snapshot.toJSON()
await fs.writeFile ("../serversettings.json", temp, function(err) {
if (err) throw err;
console.log('done');
})
})
How to read file, search for string and delete line in node.js? I have tried
var fs = require('fs')
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];
});
Let's say we have a text file, shuffle.txt contains the following content
john
doe
user1
some keyword
last word
Now we read the shuffle.txt file and then search for 'user1' keyword. If any line contains the 'user1', then we will remove the line.
var fs = require('fs')
fs.readFile('shuffle.txt', {encoding: 'utf-8'}, function(err, data) {
if (err) throw error;
let dataArray = data.split('\n'); // convert file data in an array
const searchKeyword = 'user1'; // we are looking for a line, contains, key word 'user1' in the file
let lastIndex = -1; // let say, we have not found the keyword
for (let index=0; index<dataArray.length; index++) {
if (dataArray[index].includes(searchKeyword)) { // check if a line contains the 'user1' keyword
lastIndex = index; // found a line includes a 'user1' keyword
break;
}
}
dataArray.splice(lastIndex, 1); // remove the keyword 'user1' from the data Array
// UPDATE FILE WITH NEW DATA
// IN CASE YOU WANT TO UPDATE THE CONTENT IN YOUR FILE
// THIS WILL REMOVE THE LINE CONTAINS 'user1' IN YOUR shuffle.txt FILE
const updatedData = dataArray.join('\n');
fs.writeFile('shuffle.txt', updatedData, (err) => {
if (err) throw err;
console.log ('Successfully updated the file data');
});
});
Here, if a line contains 'user1' keyword, we are removing the entire line. The new shuffle.txt file will be no longer contains a line with 'user1' keyword. The updated shuffle.txt file looks like
john
doe
some keyword
last word
For more information check the doc.
so i've written a function that should query everything in the files inventory of my mongodb using mongoose, but instead... doesn't. while each element is in fact being read, files.push() doesn't seem to have any effect on the array, as the array is still empty.
while i know that console.log() is not an effective way to debug, considering express still does not render the contents of the array, it is in fact not being populated.
yes, it is being called as getAllFiles(Image).
code below:
const Image = module.exports = mongoose.model('files', imageSchema);
function getAllFiles(collection) {
let files = [];
collection.find({}, (err, buns) => {
buns.forEach((bun) => {
let fin = bun.path.replace("public/", "");
files.push(fin);
console.log(fin);
});
});
console.log(files);
return files;
}
terminal output (ignore extraneous outputs):
wildflower :: src/bunnydb ยป node app.js
(node:23296) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.running on port 3000
[]
uploads/9160d961-3d9b-4dea-a39c-f79b86647408.jpg
was able to fix by adding a callback as it was running asynchronously:
function getAllFiles(collection, cb) {
let files = [];
collection.find({}, (err, buns) => {
console.log('err: ' + err);
console.log('buns: ' + buns);
buns.forEach((bun) => {
let fin = bun.path.replace("public/", "");
files.push(fin);
console.log('data: ' + fin);
});
cb(files);
});
console.log('arr: ' + files);
return files;
}
and on invocation the callback argument can be used to do stuff with the files
I have the following text file ("test.txt") that I want to manipulate in node.js:
world
food
I want to remove the first line so that food becomes the first line instead. How can I do that?
var fs = require('fs')
fs.readFile(filename, 'utf8', function(err, data)
{
if (err)
{
// check and handle err
}
// data is the file contents as a single unified string
// .split('\n') splits it at each new-line character and all splits are aggregated into an array (i.e. turns it into an array of lines)
// .slice(1) returns a view into that array starting at the second entry from the front (i.e. the first element, but slice is zero-indexed so the "first" is really the "second")
// .join() takes that array and re-concatenates it into a string
var linesExceptFirst = data.split('\n').slice(1).join('\n');
fs.writeFile(filename, linesExceptFirst, function(err, data) { if (err) {/** check and handle err */} });
});
I just came across the need to be able to exclude several lines in a file. Here's how I did it with a simple node function.
const fs = require('fs');
const removeLines = (data, lines = []) => {
return data
.split('\n')
.filter((val, idx) => lines.indexOf(idx) === -1)
.join('\n');
}
fs.readFile(fileName, 'utf8', (err, data) => {
if (err) throw err;
// remove the first line and the 5th and 6th lines in the file
fs.writeFile(fileName, removeLines(data, [0, 4, 5]), 'utf8', function(err) {
if (err) throw err;
console.log("the lines have been removed.");
});
})
use replace
const fs = require('fs');
function readWriteSync() {
var data = fs.readFileSync(filepath, 'utf-8');
// replace 'world' together with the new line character with empty
var newValue = data.replace(/world\n/, '');
fs.writeFileSync(filepath, newValue, 'utf-8');
}
I have a text file with multiple JSON documents on it, the JSON docs are not separated by new lines but instead they are separated by two new lines and a custom tag something like this
\n
#RECORD#SEPARATOR#
\n
What is the best way to read one by one on a stream, I am a complete node beginner and want to find the best way to do this.
Try something like this:
'use strict';
var fs = require('fs');
fs.readFile('yourfile.txt', function(err, data) {
if (err) {
throw err;
}
var docs = (data
.toString()
.split('\n')
.filter(function(line) {
return line.trim() !== '';
})
.map(function(line) {
// Do something else here depending on the exact format of your data
return 'A non-empty line: ' + line;
})
);
console.log(docs);
});