Doej Doej - 10 months ago 49
Bash Question

Reading words from an input file and grepping the lines containing the words from another file

I have a file containing list of 4000 words (

). Now I want to grep lines from another file (
) containing those 4000 words mentioned in the file

The shell script I wrote for the above problem is

while IFS= read -r line
# display $line or do somthing with $line
printf '%s\n' "$line"
grep $line sentence_per_line.txt >> output.txt

# tried printing the grep command to check its working or not
result=$(grep "$line" sentence_per_line.txt >> output.txt)
echo "$result"

done <"$file"

looks like this


The code is neither working nor does it show any error.


Grep has this built in:

grep -f A.txt sentence_per_line.txt > output.txt

Remarks to your code:

  • Looping over a file to execute grep/sed/awk on each line is typically an antipattern, see this Q&A.
  • If your $line parameter contains more than one word, you have to quote it (doesn't hurt anyway), or grep tries to look for the first word in a file named after the second word:

    grep "$line" sentence_per_line.txt >> output.txt
  • If you write output in a loop, don't redirect within the loop, do it outside:

    while read -r line; do
        grep "$line" sentence_per_line.txt
    done < "$file" > output.txt

    but remember, it's usually not a good idea in the first place.

  • If you'd like to write to a file and at the same time see what you're writing, you can use tee:

    grep "$line" sentence_per_line.txt | tee output.txt

    writes to output.txt and stdout.

  • If A.txt contains words which you want to match only if the complete word matches, i.e., pattern should not match longerpattern, you can use grep -wf – the -w matches only complete words.

  • If the words in A.txt aren't regular expressions, but fixed strings, you can use grep -fF – the -F option looks for fixed strings and is faster. These two can be combined: grep -WfF