My fellow monk has already provided a good perl solution. I just want to add that if you're on a unix box, your problem can be solved with a couple unix commands.
cut -f3 -d| X | sort > outx.txt; cut -f3 -d| Y | sort > outy.txt;
comm outx.txt outy.txt;
For sure. You need to put backslash in front of the pipe symbol when you intend to use it as a literal "vertical bar" character (or you could put quotes around it):
cut -f3 -d\| X | sort > outx.txt
cut -f3 -d'|' Y | sort > outy.txt