Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.