views:

44

answers:

2

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

+2  A: 

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"
Carl Norum
You're assuming he wants to grep files in subdirectories. If not, then I think you're stuck with: grep word *.txt.
pra
@pra, true - definitely better than his approach.
Carl Norum
A: 

No quotes needed I guess.

grep "word" $TESTING

works for me (Ubuntu, bash).

hudolejev