
Grep results expanded
http://www.peterbe.com/plog/grep-results-expanded/Grepexpansion.py23rd of April 2005
I find myself often running a grep command with the -n which means that when it shows the filename it also shows the line number it matched it on. Then what I often do is that I open the found file with my editor and move to that line with the gotoline feature. Here's a cute little bin Python script that expands the result a little bit. Try:
$ grep -rin 'def set' * frintranet/sequence/tests/testSequence.py:26: def setUp( self ): slimmer/tests/testSlimmer.py:37: def setUp(self): slimmer/tests/testSlimmer.py~:37: def setUp(self): slimmer/tests/testSlimmer.py.bak:42: def setUp(self):
Now, with Grepexpansion.py as executable (chmod +x Grepexpansion.py) located in the executable PATH:
$ grep -rin 'def set' * | Grepexpansion.py -------------------frintranet/sequence/tests/testSequence.py-------------------- 23 | Test SortEx . 24 | """ 25 | 26*| def setUp( self ): 27 | """ 28 | """ 29 | --------------------------slimmer/tests/testSlimmer.py-------------------------- 34 | self.time_records = records 35 | 36 | 37*| def setUp(self): 38 | self.timed_records=[] 39 | 40 | def tearDown(self):
You can get more less lines around it with an integer argument like this:
$ grep -rin 'def set' * | Grepexpansion.py 5
...which will show 5 lines of code on either side.
Putting this together took me about 20 minutes and it's only really tested in my environment. There are of course a zillion things you can do to improve it. Please have a play with it and let me know if you're adding any functionality. Something I'd like to see is colourcoding :)
UPDATE I know that this can be done in grep but I don't know how which means I would have to study the man pages. With pyhton I can just do it and personally I prefer python over bash.
Comment
Show all 5 commentsCommenting is currently disabled in Mobile version