Use a list of words to grep in an other list
Solution 1:
I'd ignore grep
for this one. It's good for regular expressions but it doesn't look like you really need that here. comm
can compare two files and show you intersections. Using your exact examples:
$ comm -12 list.txt output.txt
a.1
b.1
etc
This is faster than any grep will be but it relies (heavily) on the files being sorted. If they aren't, you can pre-sort them but that will alter the output so it's sorted too.
comm -12 <(sort list.txt) <(sort output.txt)
Alternatively, this answer from iiSeymour will let you do it with grep
. The flags ask for an input file and force a fixed-string, full-word search. This won't rely on order but will be based on the output.txt
order. Reverse the files if you want them in the order of the list.txt.
$ grep -wFf list.txt output.txt
a.1
b.1
etc
If your list.txt
is really big, you might have to tackle this a little more iteratively and pass each line to grep separately. This will massively increase processing time. In the above you'd be reading output.txt
once, but this way you'd read and process it for every list.txt line. It's horrible... But it might be your only choice. On the upside, it does then sort things by the list.txt
order.
$ while read line; do grep -wF "$line" output.txt; done < list.txt
a.1
b.1
etc