To uninstall packages from B that are not on A, (which I wouldn't bother doing, as it will make little difference in space) -
Just change things around a bit. Which ever command you use to get the list of packages on A (dpkg --list or dpkg --get-selections), use on B, and make the output name slightly different, like packlist2.txt, or file2.list. Then run a diff command on the 2 lists, packagelist.txt and packagelist2.txt, or file.list and file2.list. Save the output of the diff command to another file, say remove.list, then ultimately, do
apt-get -u remove 'cat remove.list'
You may do something very bad by doing that as well. I would carefully go through the removal.list and see what those packages are. I doubt both machines have identical hardware. Taking something off B is going to seem like a really stupid thing when it turns out to have something to do with having a functional network card. Long story short, I wouldn't bother with the removal step at all. I didn't give verbose removal instructions, just guidelines, so you'll have to think about why you're doing this, and if anything good will come out of it.