3

I have a file in which the data is in the form like

abc@email.com:name
ewdfgwed@gmail.com:nameother
wertgtr@gmsi.com:onemorename

I want to store the emails and names in arrays like

email = ["abc@email.com","ewdfgwed@gmail.com","wertgtr@gmsi.com"]

names = ["name","nameother","onemorename"]

Also, guys, the file is a little bit large around 50 MB so also I want to do it without using a lot of resources

I have tried this to work but can't make things done

    // read contents of the file
    const data = fs.readFileSync('file.txt', 'UTF-8');

    // split the contents by new line
    const lines = data.split(/\r?\n/);

    // print all lines
    lines.forEach((line) => {
       names[num] = line;
        num++
    });
} catch (err) {
    console.error(err);
}
Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
For This
  • 153
  • 1
  • 2
  • 8
  • You might start by looking at [split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) and [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map). You can pretty much do the whole thing with these two tools. You'll probably want something like `textVariable.split(/[\r\n]+/)` to get an array of lines, and then you can use `.map` to get the stuff before or after the `:`. Maybe give it a try, and if you get stuck post your code and ask for help? – David784 Apr 01 '20 at 16:38
  • 1
    Welcome, @For This! Please make sure to read the "How to Ask " section: https://stackoverflow.com/help/how-to-ask This is likely going to get closed, as it isn't really asking a specific question, but instead, seems to be asking for someone to write some code FOR you. Try to include what you've tried, what you think might work, but isn't...etc. – Dave Apr 01 '20 at 16:42
  • 2
    please check [mcve] and share an example of what you have tried so far – nircraft Apr 01 '20 at 16:43
  • 1
    Does this answer your question? [Read a file one line at a time in node.js?](https://stackoverflow.com/questions/6156501/read-a-file-one-line-at-a-time-in-node-js) – Cameron Little Apr 01 '20 at 17:34

2 Answers2

5

Maybe this will help you.

Async Version:

const fs = require('fs')

const emails = [];
const names = [];

fs.readFile('file.txt', (err, file) => {

  if (err) throw err;

  file.toString().split('\n').forEach(line => {
    const splitedLine = line.split(':');

    emails.push(splitedLine[0]);
    names.push(splitedLine[1]);

  });
});

Sync Version:

const fs = require('fs')

const emails = [];
const names = [];

fs.readFileSync('file.txt').toString().split('\n').forEach(line => {
  const splitedLine = line.split(':');

  emails.push(splitedLine[0]);
  names.push(splitedLine[1]);
})

console.log(emails)
console.log(names)
0

You can directly use line-reader :

fileData.js :

const lineReader = require('line-reader');
class FileData {

    constructor(filePath) {
        this.emails = [];
        this.names = [];

        lineReader.eachLine(filePath, function(line) {
            console.log(line);
            const splitedLine = line.split(':');
            emails.push(splitedLine[0]);
            names.push(splitedLine[1]);
        });
    }

    getEmails(){
        return this.emails;
    }

    getNames(){
        return this.names;
    }   


}

module.exports = FileData

Whrerever You want:

const FileData = require('path to fileData.js');
const fileData = new FileData('test.txt');
console.log(fileData.getEmails())
Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
  • The problem here in it is that I can only access those values inside the function but i want to use it on a global scope – For This Apr 02 '20 at 08:20