How do I search for multiple strings in any order across multiple lines?

0

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

wn-

Posted 2011-07-13T20:18:53.733

Reputation: 111

Can you maybe give an example? – slhck – 2011-07-13T20:23:59.510

Python Script, FTW! – James T Snell – 2011-07-13T20:36:49.087

Answers

0

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

Slartibartfast

Posted 2011-07-13T20:18:53.733

Reputation: 6 899

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 – 2011-07-14T12:55:30.587

Yeah, worksish with barlop's addition...but still not exactly what one would want. – wn- – 2011-07-14T21:13:09.963