Using Raku (formerly known as Perl_6)
Below is a general solution for matching lines of one file (saved as @a array) against lines of a second file (saved as @b array), counting occurrences (i.e. Bagging):
raku -e 'my @a = dir(test => "alphabet.txt").IO.lines.reverse; my @b = $*ARGFILES.lines; \
for @a -> $a {@b.grep(/<$a>/).Bag.pairs.say};' alphabet.txt alphabet.txt
In constructing @a, Raku is given a dir() location and a test => "…" filename. In constructing @b, one-or-more files are entered on the command line, and read off via Raku's $*ARGFILES dynamic variable.
General Input is alphabet.txt, one letter per line and reversed immediately upon reading into Raku to place the array in "z".."a" order;
General Output (when two copies of "a".."z" alphabet.txt are entered on the command-line):
(z => 2)
(y => 2)
(x => 2)
(w => 2)
(v => 2)
(u => 2)
(t => 2)
(s => 2)
(r => 2)
(q => 2)
(p => 2)
(o => 2)
(n => 2)
(m => 2)
(l => 2)
(k => 2)
(j => 2)
(i => 2)
(h => 2)
(g => 2)
(f => 2)
(e => 2)
(d => 2)
(c => 2)
(b => 2)
(a => 2)
Note how the return stays in the same order as the @a array, and how Raku doesn't require a sort call to produce the output above.
Finally, solving the OP's issue, all that has to be changed from the code above is using my @b = $*ARGFILES.lines.words instead of my @b = $*ARGFILES.lines.
[To obtain tab-separated output use .put instead of .say in the code above. This drops the surrounding parens and the => arrow between the two columns].
Final Code:
~$ raku -e 'my @a = dir(test => "dog_apple_cat.txt").IO.lines.grep(*.chars); \
my @b = $*ARGFILES.lines.words; for @a -> $a { \
@b.grep(/<$a>/).Bag.pairs.put};' text.txt
dog 1
apple. 1
cat 2
https://docs.raku.org/type/Bag
https://raku.org
dog-fishandpineappleand contiguous words likedog dogin file2 and at least 1 case in file1 that doesn't appear in file2, e.g.rabbit. If you only show trivial sunny day cases in your example then you greatly increase your chances of getting an answer that only works for trivial sunny day cases.