0
Here i want to print line which contains following strings: 
Object.< anonymous > 

Here is the multiple lines: now find those lines who contains substring Object . < anonymous >

Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'yourusername'@'localhost' (using password: YES)
at Socket.< anonymous > (/home/abc/Desktop/AJ/CustomLogger/node_modules/mysql/lib/Connection.js:91:28)
at Object.< anonymous > (/home/abc/Desktop/AJ/CustomLogger/conn.js:10:5)
at Module._compile (internal/modules/cjs/loader.js:686:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)

Output should be : at Object.< anonymous > (/home/abc/Desktop/AJ/CustomLogger/conn.js:10:5)

Atharva Jawalkar
  • 166
  • 1
  • 12

1 Answers1

2

Either you can split your string and get all lines first and then check for Object.< anonymous > (with or without regex) to filter them out.

let str = `Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'yourusername'@'localhost' (using password: YES)
at Socket.< anonymous > (/home/abc/Desktop/AJ/CustomLogger/node_modules/mysql/lib/Connection.js:91:28)
at Object.< anonymous > (/home/abc/Desktop/AJ/CustomLogger/conn.js:10:5)
at Module._compile (internal/modules/cjs/loader.js:686:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)`,

res = str.split("\n").filter(line=>line.indexOf("Object.< anonymous >")>-1);

console.log(res);

Or, you can match with a regex with multiline flag (m)

let str = `Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'yourusername'@'localhost' (using password: YES)
at Socket.< anonymous > (/home/abc/Desktop/AJ/CustomLogger/node_modules/mysql/lib/Connection.js:91:28)
at Object.< anonymous > (/home/abc/Desktop/AJ/CustomLogger/conn.js:10:5)
at Module._compile (internal/modules/cjs/loader.js:686:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)`,

res = str.match(/^(.*Object\.< anonymous >.*)$/mg);

console.log(res);
Koushik Chatterjee
  • 4,106
  • 3
  • 18
  • 32