Doej
Doej

Reputation: 49

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 (A.txt). Now I want to grep lines from another file (sentence_per_line.txt) containing those 4000 words mentioned in the file A.txt.

The shell script I wrote for the above problem is

#!/bin/bash
file="A.txt"
while IFS= read -r line
do
        # 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"

And A.txt looks like this

applicable
available
White
Black
..

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

Upvotes: 1

Views: 2160

Answers (1)

Benjamin W.
Benjamin W.

Reputation: 52556

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

Upvotes: 2

Related Questions