0

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.

1
  • You must have missed something, if "-a or --text flags seems to be causing matches on all the lines". If you have some sample code and input, we might understand the problem better... Commented Jan 19, 2016 at 22:40

2 Answers 2

1

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

for i in $(find . -name '*.txt'); do
    cat $i | tr -d '\000' | grep findThisStr
done
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.