Reputation: 5247
I want grep for a particular work in multiple files. Multiple files are stored in variable testing.
TESTING=$(ls -tr *.txt)
echo $TESTING
test.txt ab.txt bc.txt
grep "word" "$TESTING"
grep: can't open test.txt
ab.txt
bc.txt
Giving me an error. Is there any other way to do it other than for loop
Upvotes: 0
Views: 287
Reputation: 6018
No quotes needed I guess.
grep "word" $TESTING
works for me (Ubuntu, bash).
Upvotes: 0
Reputation: 224954
Take the double quotes out from around $TESTING
.
grep "word" $TESTING
The double quotes are making your whole file list expand to a single argument to grep
. The right way to do this is:
find . -name \*.txt -print0 | xargs -0 grep "word"
Upvotes: 2