George Murphy
George Murphy

Reputation: 1107

Run recursive grep using two patterns

How can I use this grep pattern to recursively search a directory? I need for both of these to be on the same line in the file the string. I keep getting the message back this is a directory. How can I make it search recursively all files with the extension .cfc?

"<cffunction" and "inject="
grep -insR "<cffunction" | grep "inject=" /c/mydirectory/

Upvotes: 0

Views: 456

Answers (2)

zwer
zwer

Reputation: 25809

You've got it backwards, you should pipe your file search to the second command, like:

grep -nisr "inject=" /c/mydirectory | grep "<cffunction"

edit: to exclude some directories and search only in *.cfc files, use:

grep -nisr --exclude-dir={/abs/dir/path,rel/dir/path} --include \*.cfc "inject=" /c/mydirectory | grep "<cffunction"

Upvotes: 0

codeforester
codeforester

Reputation: 43039

Use find and exec:

find your_dir -name "*.cfc" -type f -exec grep -insE 'inject=.*<cffunction|<cffunction.*inject=' /dev/null {} +
  • find finds your *.cfc files recursively and feeds into grep, picking only regular files (-type f)
  • inject=.*<cffunction|<cffunction.*inject= catches lines that have your patterns in either order
  • {} + ensures each invocation of grep gets up to ARG_MAX files
  • /dev/null argument to grep ensures that the output is prefixed with the name of file even when there is a single *.cfc file

Upvotes: 1

Related Questions