Asterlux
Asterlux

Reputation: 178

How to read strings from a text file and use them in grep?

I have a file of strings that I need to search for in another file. So I've tried the following code:

#!/bin/bash

while read name; do
   #echo $name
   grep "$name" file.txt > results.txt
done < missing.txt

The echo line confirms the file is being read into the variable, but my results file is always empty. Doing the grep command on its own works, I'm obviously missing something very basic here but I have been stuck for a while and can't figure it out.

I've also tried without quotes around the variable. Can someone tell me what I'm missing? Thanks a bunch

Edit - input file was DOS format, set file format to unix and works fine now

Upvotes: 1

Views: 4352

Answers (2)

glenn jackman
glenn jackman

Reputation: 246807

Use grep's -f option: Then you only need a single grep call and no loop.

grep -f missing.txt file.txt > results.txt

If the contents of "missing.txt" are fixed strings, not regular expressions, this will speed up the process:

grep -F -f missing.txt file.txt > results.txt

And if you want to find the words of missing.txt in the other file, not partial words

grep -F -w -f missing.txt file.txt > results.txt

Upvotes: 6

lael.nasan
lael.nasan

Reputation: 71

My first guess is that you are overwriting your results.txt file in every iteration of the while loop (with the single >). If it is the case you should at least have the result for the very last line in your missing.txt file. Then I think it would suffice to do something like

#!/bin/bash
 
while read name; do
   #echo "$name"
   grep "$name" file.txt
done < missing.txt > results.txt

Upvotes: 2

Related Questions