Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep files with uncommented "console.log" string

Tags:

regex

grep

unix

I need to return all files in a directory which contain the string console.log uncommented. So I think I should search for any line which does not have the string // between the newline and the console.log string.

This command returns both commented and uncommented console.log. I want to exclude instances where console.log is commented.

grep -H -r  "^.*[^//].*console\.log" /var/www/vhosts/mysite.com

The command I need would match these lines:

var num = 0;  console.log(num);
alert('some text'); console.log('anything');
  console.log();

but not match these lines:

var num = 0; // console.log(num);
//alert('some text'); console.log('anything');
  // console.log();
like image 620
steampowered Avatar asked Jun 20 '26 11:06

steampowered


2 Answers

Use -L switch

grep -rL '//.*console\.log' directory

Update:

grep -rlP '^(?!.*//.*console\.log)(?=.*console\.log)' directory
like image 172
Ωmega Avatar answered Jun 22 '26 00:06

Ωmega


Extending Ωmega's answer: In order to find mulitline comments (/* console.log(); */) additionally use:

^(?!.*//.*console\.log)^(?!.*/\*.*console\.log)(?=.*console\.log)
like image 22
John Archer Avatar answered Jun 22 '26 00:06

John Archer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!