Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working in a large codebase and want to see where particular strings like "foo" and "bar" are used, within n lines of each other, in any file in that codebase.

Ideally I'd use a built-in Unix command, but a Python script would be ok too. (Perl or Ruby would not be great).

share|improve this question
    
Can you maybe give an example? – slhck Jul 13 '11 at 20:23
    
Python Script, FTW! – Doc Jul 13 '11 at 20:36

Looking for foo and bar within 5 lines of each other:

grep -C5 foo inputfile | grep bar > outputfile

Add copying for 'large code base':

find /my/codebase/path -type f -print0 | xargs -0 grep -C5 foo | grep bar > outputfile
share|improve this answer
    
Doing as you have "grep -C5 foo inputfile | grep bar > outputfile" will ultimately only display/show lines with bar. How about adding -C5 between grep and bar. So | grep -C5 bar that probably works better than your version. – barlop Jul 14 '11 at 12:55
    
Yeah, worksish with barlop's addition...but still not exactly what one would want. – wn- Jul 14 '11 at 21:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .