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).

link|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
feedback

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
link|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
feedback

Your Answer

 
or
required, but never shown

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