Grep and Python

I need a way of searching a file using grep via a regular expression from the Unix command line. For example when I type in the command line: I need the regular expression ‘RE’ to be searched in the file and print out the matching lines. Here’s the code I have: But when I enter a word … Read more

How can I exclude directories from grep -R?

SOLUTION 1 (combine find and grep) The purpose of this solution is not to deal with grep performance but to show a portable solution : should also work with busybox or GNU version older than 2.5. Use find, for excluding directories foo and bar : Then combine find and the non-recursive use of grep, as a portable solution : SOLUTION 2 (using the –exclude-dir option of grep): … Read more

How can I exclude directories from grep -R?

SOLUTION 1 (combine find and grep) The purpose of this solution is not to deal with grep performance but to show a portable solution : should also work with busybox or GNU version older than 2.5. Use find, for excluding directories foo and bar : Then combine find and the non-recursive use of grep, as a portable solution : SOLUTION 2 (using the –exclude-dir option of grep): … Read more

How do I grep recursively?

The first parameter represents the regular expression to search for, while the second one represents the directory that should be searched. In this case, . means the current directory. Note: This works for GNU grep, and on some platforms like Solaris you must specifically use GNU grep as opposed to legacy implementation. For Solaris this is the ggrep command.

grep –ignore-case –only

This is a known bug on the initial 2.5.1, and has been fixed in early 2007 (Redhat 2.5.1-5) according to the bug reports. Unfortunately Apple is still using 2.5.1 even on Mac OS X 10.7.2. You could get a newer version via Homebrew (3.0) or MacPorts (2.26) or fink (3.0-1). Edit: Apparently it has been fixed on OS X 10.11 (or maybe earlier), even … Read more

PowerShell equivalent to grep -f

The -Pattern parameter in Select-String supports an array of patterns. So the one you’re looking for is: This searches through the textfile doc.txt by using every regex(one per line) in regex.txt