$ grep -n -x -F -f fileA.txt fileB.txt | datamash -s -t : -g 2 collapse 1
This first uses
grep to get the lines from
fileB.txt that exactly matches the lines in
fileA.txt, and outputs the matching line numbers along with the lines themselves.
-F in addition to the options that are used in the question. I do this to avoid reading the patterns from
fileA.txt as regular expressions (
-F), and to match complete lines, not substrings (
datamash utility is then parsing this as lines of
:-delimited fields (
-t :), sorting it (
-s) on the second field (
-g 2; the countries) and collapsing the first field (
collapse 1; the line numbers) into a list for each country.
You could then obviously replace the colons and commas with tabs using
tr ':,' '\t\t', or with spaces in a similar way.
$ grep -n -x -f fileA.txt -F fileB.txt | datamash -s -t : -g 2 collapse 1 | tr ':,' '\t\t'
Germany 4 9
UK 5 6
USA 1 2 11