Nate Jenson
Nate Jenson

Reputation: 2794

Recursive grep with piped input

I'm trying to recursively grep all of the .txt files within a directory:

grep -r --include \*.txt findThisStr .

Unfortunately, the .txt files I'm trying to search have some null characters in them and using the -a or --text flags seems to be causing matches on all the lines in the file. To remedy this, I was hoping to sanitize the input and pipe it through grep (like this), but I'm unsure how to do this recursively, or even if it's possible.

Essentially, I'd like to do something like this, except file would be dynamic:

   cat file | tr -d '\000' | grep -r --include \*.txt findThisStr .

I'm using grep on Windows 7 through the 64-bit MINGW shell.

Upvotes: 0

Views: 300

Answers (2)

John
John

Reputation: 336

find . -name '*.txt' |xargs grep -l findThisStr

Will find the files named .txt and print out the filenames with findThisStr in them. If you have special characters in findThisStr you should escape or quote them.

Upvotes: 0

Dirk Herrmann
Dirk Herrmann

Reputation: 5939

You can use a loop over all *.txt files:

for i in $(find . -name '*.txt'); do
    cat $i | tr -d '\000' | grep findThisStr
done

Upvotes: 1

Related Questions