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.

Previous:
Serious flaw in Bose headphones April 23, 2005 Music
Next:
Gmail shortcuts April 26, 2005 Linux
Related by category:
A Python dict that can report which keys you did not use June 12, 2025 Python
Faster way to sum an integer series in Python August 28, 2025 Python
Combining Django signals with in-memory LRU cache August 9, 2025 Python
Native connection pooling in Django 5 with PostgreSQL June 25, 2025 Python
Related by keyword:
The best grep tool in the world; ripgrep June 19, 2018 Linux, Web development, macOS
gg - wrapping git-grep August 11, 2009 Linux
Redirect stderr into becoming dots in Bash September 2, 2006 Linux
Find print statements in Python code April 12, 2005 Python, Linux