Remove everything after a certain character in txt file

Viewed 385

hello ı m new to coding , ı need your help about something , ı m trying to filter a txt file and make a list in a format i like

here is what original txt file looks like :

pepitbeng:davy141089 | LV: 5 | BE: 1017 | RP: 400 | Refunds: 3 | Champs: 1 | Skins: 0 | Email Verified: true | Lastplay: Error
korvin918:M5al3elu2z6k | LV: 41 | BE: 2065 | RP: 23 | Refunds: 1 | Champs: 57 | Skins: 23 | Email Verified: true | Lastplay: 1/11/2019 7:02:15 PM
monkeyshadowtms:apolo2002 | LV: 21 | BE: 6795 | RP: 0 | Refunds: 3 | Champs: 10 | Skins: 0 | Email Verified: true | Lastplay: 7/25/2019 5:00:15 PM

and there are thousands of them

what ı like to do is line by line delete everything after space so only id and password left , end result will looks like this

pepitbeng:davy141089
korvin918:M5al3elu2z6k
monkeyshadowtms:apolo2002

ı try few things but can only get first line ,

var fs = require('fs');
var textByLine = fs.readFileSync('1.txt').toString().split(" ");

console.log(textByLine[0]);

this way ı can get pepitbeng:davy141089 but cant get to second line because everything is deleted after them so how can ı get 0 array of every line

ı also try this

var fs = require('fs');
var textByLine = fs.readFileSync('1.txt').toString().split("\n");

console.log(textByLine[0]);

this way ı can get line by line but whole part result of above code

pepitbeng:davy141089 | LV: 5 | BE: 1017 | RP: 400 | Refunds: 3 | Champs: 1 | Skins: 0 | Email Verified: true | Lastplay: Error

ı feel like ı should use forEach() function but ı dont know how to implement to this waiting for your response thanks.

sorry to bother ı manage to solve

var fs = require('fs');
var textByLine = fs.readFileSync('1.txt').toString().split("\n");

console.log(textByLine[0].split(" ")[0]);

hopefully help someone else

1 Answers

You can use a regular expression to match non-spaces at the beginning of the line:

var fs = require('fs');
var lines = fs.readFileSync('1.txt')
  .toString()
  .split("\n")
  .map(line => line.match(/\S*/)[0]);

\S matches a non-space character, and the * repeater matches as many of those characters in a row as it can.

const text = `pepitbeng:davy141089 | LV: 5 | BE: 1017 | RP: 400 | Refunds: 3 | Champs: 1 | Skins: 0 | Email Verified: true | Lastplay: Error
korvin918:M5al3elu2z6k | LV: 41 | BE: 2065 | RP: 23 | Refunds: 1 | Champs: 57 | Skins: 23 | Email Verified: true | Lastplay: 1/11/2019 7:02:15 PM
monkeyshadowtms:apolo2002 | LV: 21 | BE: 6795 | RP: 0 | Refunds: 3 | Champs: 10 | Skins: 0 | Email Verified: true | Lastplay: 7/25/2019 5:00:15 PM`;
var lines = text
  .split("\n")
  .map(line => line.match(/\S*/)[0]);
console.log(lines);

If your output needs to be a string instead, then replace everything past a space with the empty string:

const text = `pepitbeng:davy141089 | LV: 5 | BE: 1017 | RP: 400 | Refunds: 3 | Champs: 1 | Skins: 0 | Email Verified: true | Lastplay: Error
korvin918:M5al3elu2z6k | LV: 41 | BE: 2065 | RP: 23 | Refunds: 1 | Champs: 57 | Skins: 23 | Email Verified: true | Lastplay: 1/11/2019 7:02:15 PM
monkeyshadowtms:apolo2002 | LV: 21 | BE: 6795 | RP: 0 | Refunds: 3 | Champs: 10 | Skins: 0 | Email Verified: true | Lastplay: 7/25/2019 5:00:15 PM`;
const newText = text.replace(/ .*/g, '');
console.log(newText);

Related