I used to have a script like the following
for i in $(cat list.txt)
do
grep $i sales.txt
done
Where cat list.txt
tomatoes
peppers
onions
And cat sales.txt
Price Products
$8.88 bread
$6.75 tomatoes
$3.34 fish
$5.57 peppers
$0.95 beans
$4.56 onions
I am a beginner in BASH/SHELL and after reading posts like Why is using a shell loop to process text considered bad practice? I changed the previous script to the following:
grep -f list.txt sales.txt
Is this last way of doing it really better than using a for loop? At first I thought it was, but then I realized it is probably the same since grep has to read the query file each time it greps a different line in the target file. Does anyone know if its actually better and why? If its better somehow I'm probably missing something about how grep processes this task but I can't figure it out.
grep -f list.txt sales.txt
wouldn't be considered the "better" option. I suppose if you needed some intermediate processing between switching through your patterns inlist.txt
and grepping, then maybe a loop depending on what that was... maybe...