Reputation: 25
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
Upvotes: 1
Views: 1915
Reputation: 47099
Sounds like a job for GNU parallel:
parallel --dry-run grep '" BA "' '{} > otherdir/{/}' ::: dir/{a,b,c}.txt
Output:
grep " BA " dir/a.txt > otherdir/a.txt
grep " BA " dir/b.txt > otherdir/b.txt
grep " BA " dir/c.txt > otherdir/c.txt
Remove --dry-run
when you're happy with what you see.
{}
is replaced by the inputs after :::
(these can also come from stdin or a file), {/}
is the basename of {}
.
Upvotes: 1
Reputation: 272297
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
Upvotes: 2