How do I send the grep results to a different output file for each input file?

advertisements

I have a folder that contains text files. I need to extract lines that has 'BA' from these text files . I used grep command to print the lines with BA. I would like to save the outputs to another folder with the same file names. How can I change the following code?

grep "  BA  "  dir/*.txt


for i in dir/*.txt; do
   grep "  BA  " $i > $newdir/`basename $i`
done

Note the use of basename, which takes dir/a.txt (say) and returns a.txt