I have a file containing list of 4000 words (
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)
Grep has this built in:
grep -f A.txt sentence_per_line.txt > output.txt
Remarks to your code:
$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
grep "$line" sentence_per_line.txt | tee output.txt
output.txt and stdout.
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.
A.txtaren't regular expressions, but fixed strings, you can use
grep -fF– the
-Foption looks for fixed strings and is faster. These two can be combined: