Grep results expanded

23 April 2005   5 comments   Python, Linux

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

Mind That Age!

This blog post is 13 years old! Most likely, its content is outdated. Especially if it's technical.

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

Previous:
Serious flaw in Bose headphones 23 April 2005
Next:
Gmail shortcuts 26 April 2005
Related by Keyword:
The best grep tool in the world; ripgrep 19 June 2018
gg - wrapping git-grep 11 August 2009
Redirect stderr into becoming dots in Bash 02 September 2006
Find print statements in Python code 12 April 2005
Grep in Jed 27 January 2004
Related by Text:
jQuery and Highslide JS 08 January 2008
I'm back! Peterbe.com has been renewed 05 June 2005
Anti-McCain propaganda videos 12 August 2008
Ever wondered how much $87 Billion is? 04 November 2003
Guake, not Yakuake or Yeahconsole 23 January 2010