Extracting/condensing text from multiple files to multiples files

Hi Everyone,

I'm really new to all this so I'm really hoping someone can help. I have a directory with ~1000 lists from which I want to extract lines from and write to new files. For simplicity lets say they are shopping lists and I want to write out the lines corresponding to apples to a new file for each list i.e. list1apples, list2apples etc. So far the best I've come up with is to use grep on each individual list:

grep Apples list1.text > list1apples.txt
grep Apples list2.text > list2apples.txt

Is there an easier way of doing this so that I can extract the required text from each document to a new one without having to modify the command for each one? As I said this is simplified example, in reality each of the lists has a completely different name given by 4 random letters and numbers.

Alternatively finding a way of deleting everything from these files apart from the desire text would also work.

Any help would be greatly appreciated!

for i in list*.text
do
grep Apples "$i" > ${i%.*}apples.txt
done
1 Like

Brilliant. Easily modified this for my files. Thanks!