In one text file, I have 150 words. I have another text file, which has about 100,000 lines.
How can I check for each of the words belonging to the first file whether it is in the second or not?
I thought about using grep
, but I could not find out how to use it to read each of the words in the original text.
Is there any way to do this using awk
? Or another solution?
I tried with this shell script, but it matches almost every line:
#!/usr/bin/env sh
cat words.txt | while read line; do
if grep -F "$FILENAME" text.txt
then
echo "Se encontró $line"
fi
done
Another way I found is:
fgrep -w -o -f "words.txt" "text.txt"
You can use
grep -f
:OR else to match full words:
UPDATE: As per the comments:
Use grep like this:
SECOND OPTION
Thank you to Ed Morton for pointing out that the words in the file "reserved" are treated as patterns. If that is an issue - it may or may not be - the OP can maybe use something like this which doesn't use patterns:
File "reserved"
and file "text"
Awk script is like this:
with output:
THIRD OPTION
Alternatively, it can be done quite simply, but more slowly in bash: