Last Updated: February 25, 2016
·
556
· tony

Grep and AG - file searching, ignoring and multiple parameters (also benchmarking)

This is a quick one (at work). I will expand on it more later:

Use grep(1):

$ time grep -R -C 2 --exclude-dir={.git,log,node_modules,vendor,tmp,public} -E 'can_sync_with|sync_eligible' .
./app/decorators/duh/planet_decorator.rb-    end
./app/decorators/duh/planet_decorator.rb-
./app/decorators/duh/planet_decorator.rb:    def sync_eligible
./app/decorators/duh/planet_decorator.rb:      Planet.can_sync_with(self).alphabetical
./app/decorators/duh/planet_decorator.rb-    end
./app/decorators/duh/planet_decorator.rb-
--
--
./app/decorators/duh/planet_decorator.rb-    end
./app/decorators/duh/planet_decorator.rb-
./app/decorators/duh/planet_decorator.rb:    def self.can_sync_with(plan)
./app/decorators/duh/planet_decorator.rb-      prov.npi.blank? ? all_except(plan) : all_except(plan).without_npi
./app/decorators/duh/planet_decorator.rb-    end
grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn} -R -C 2  -E  .  0.66s user 0.02s system 98% cpu 0.691 total

Or use thesilversearcher / ag(1) for a faster search:

$ time ag -C 2 --ignore-dir={.git,log,node_modules,vendor,tmp,public} 'merge_eligible|can_merge_with' .
app/decorators/duh/planet_decorator.rb
43-    end
44-
45:    def sync_eligible
46:      Planet.can_sync_with(self).alphabetical
47-    end
48-
--
85-    end
86-
87:    def self.can_sync_with(plan)
88-      prov.npi.blank? ? all_except(plan) : all_except(plan).without_npi
89-    end
ag -C 2 --ignore-dir={.git,log,node_modules,vendor,tmp,public}  .  0.01s user 0.03s system 167% cpu 0.028 total