URL: https://www.peterbe.com/plog/grep-results-expanded/Grepexpansion.py

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.

Comments

Alex

You might be interested in the -A, -B and -C options to grep. These will place matching lines in a specified amount of context.

James Thiele

% man grep
<snip>
-C [NUM], -NUM, --context[=NUM]
Print NUM lines (default 2) of output context.
<snip>

Martin Blais

hi peterbe
you're reinventing the wheel again.

if you want the editor, you can use grep from within emacs and then next-error / previous-error to automatically open the files and navigate.

an idea: if you're interested in controlling a running instance of emacs from an external program, you can send arbitrary lisp commands using emacsclient thru a socket, say (file-file "filename") (goto-line 67) (recenter). you could even write a GUI program, binding it with emacs in tihs way. if you're interested in fiddling with that, i have some prototype code that demonstrates how to do this.

cheers,

Peter Bengtsson

I *know* that I'm reinventing the wheel; but this time the wheel is more flash. With python *I* get more control plus as a script, it's more reusable than what I could accomplish with bash.

Jed has something called grep_mode which I haven't been using for a long time. And since Jed is my editor of choice, that's where I'll be looking for fancy personal solutions :)

Torsten Will

I would suggest emacs for that purpose. Menu: Tools, Grep. Then, pressing F9 cycles through all matches in your editor. Maybe vi can do it similar...

Your email will never ever be published.

Related posts