Wanna grep wrapper like mine?
17th of July 2006
If you, like me, use grep a lot to search through your source code you might already have your own personalised, ad-hoc way of using grep. If you don't let me show you how I do it.
First of all since I do more than ten greps a day I don't want to type too much every time. That's why I've created a little script called just g and this is what it looks like in action for me:
peterbe@trillian:~/MExpenses $ g '\.sendEmail' --------------------------------- Cronjobs.py ---------------------------------- 95 | # send it to the user 96*| self.sendEmail(message, email, self.getWebmasterEmail(), 97 | subject, debug=DEBUG) --------------------------------- Homepage.py ---------------------------------- 660 | try: 661*| self.sendEmail(msg, sendto, sendfrom, subject, 662 | swallowerrors=True 792 | comment=comment, matter=matter) 793*| self.sendEmail(msg, self.getWebmasterEmail(), self.getWebmasterFromfield(), 794 | "Email from Contact Us page", ------------------------------- Registration.py -------------------------------- 221 | # send the email 222*| self.sendEmail(mailmessage, user.email, self.getWebmasterEmail(), 223 | subject, 669 | 670*| self.sendEmail(msg, user.email, self.getWebmasterEmail(), 671 | subject,
(nb. I've had to slightly modify the output to fit this page)
That's what it looks like.
First of all I like to remove all junk that isn't the right file anyway. For example files like home.html.bak, foo.pyc (byte compiled Python files) or screen.css~ (autosaved backup files in jed) are filtered out and ignored. Secondly, the matched lines are shown with one line of content before and after to put the found line in context. Lastly, multiple findings in the same file shows only the filename once.
It takes some getting used to (old habits die slowly) but once you do, it's a really convenient little timesaver. Here's how I did it.
Here's the script g in my home bin directory:
peterbe@trillian:~ $ cat ~/bin/g
#!/usr/bin/python
import sys, os
args = sys.argv[1:]
i = False
if '-i' in args:
i = True
args.remove('-i')
if i:
cmd = "grep -rin '%s' * | Grepexpansion.py" % args[0]
else:
cmd = "grep -rn '%s' * | Grepexpansion.py" % args[0]
os.system(cmd)
As you can see, it basically executes a plain grep command and pipes it into another program called Grepexpansion.py. Download Grepexpansion.py and put this in your ~/bin directory too like this:
$ cd ~/bin $ wget http://www.fry-it.com/at/g/Grepexpansion.py $ chmod +x Grepexpansion.py $ wget http://www.fry-it.com/at/g/g $ chmod +x g
I hope it works for you too and I hope it helps you shave off a few milliseconds of time and frustration :)
Pretty cool, the format reminds me of MoinMoin's full text search. To get it to work on Windows (+ Cygwin) change the 'cmd' lines to use "python \\path\\to\\grepexpansion.py" instead of just the script name. Also, changing the last line in grepexpansion.py to:
print "%s% 4s| %s" % (star, i, content[i-1]),
Lines up the output a little better :)