This is also incorrect in several ways.
for i in `cat file_1.txt`; do grep $i file_2.txt -A 1; done
1) Don't Read Lines With For
. Use a while+read
2) Useless Use Of Cat
. Although in this case it's really part and parcel of #1.
3) QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS
. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.
4) In addition, while not wrong, per se, $(..) is highly recommended over `..`
. Backticks are deprecated syntax.
while IFS='' read -r entry ; do
grep -A 1 "$entry" file_2.txt
I added IFS=''
to preserve leading and trailing whitespace in the file. Leave this off if this isn't an issue, or if you want it removed.
is probably still the best option, however, although you need to be sure the matching patterns are exact, as millgates mentioned. Another thing to watch out for is that your files shouldn't have dos-style line endings. The extra carriage return would generally make matches fail.