compare two files and search keyword and print output

You have two files to compare by searching keyword from one file into another file

File A
23   >pp_ANSWER 
24   >aa  hello  [Beta done ]
25    >jau  head wear [John mayor]
66    >jss oops [calix meyers]
872   >aqq olps ploww oww sss [Mutton deer]
722   >GG_KILLER
..... large files

File B
Beta done 
KILLER
John Mayor
calix meyers

Desired output
24   >aa  hello  [Beta done ]
25    >jau  head wear [John mayor]
66    >jss oops [calix meyers]
722   >GG_KILLER


awk 'NR==FNR{a[$1]=$1;next}$0 in a{print $1,a[$1],$2}'
thought like any keyword will match will come but not working
>outputFile
cat FileB | while read line
do
grep "$line" FileA >> outputFile
done
1 Like
grep -f fileB fileA
1 Like

This is just

grep -f FileB FileA > outputFile

Some implementations may use fgrep instead though, depending what flags are supported.

The code in the previous post could be very slow depending on the data volumes, however it could be adapted to include the square brackets or force other conditions on the search for the string. It depends what you are actually after.

I hope that this helps. Let us all know if I have missed the point.

Robin
Liverpool/Blackburn
UK

---------- Post updated at 06:13 AM ---------- Previous update was at 06:12 AM ----------

U r Right
fgrep is faster here while running this compared as comapred to grep
Thanks

perl -ne 'BEGIN{open (F,"FileA");@f=; } chomp($_);foreach $k (@f){chomp($k); print "\n$k" if ($k=~ m/$_/);}' FileB