#!/usr/bin/perl -w use strict; $\ = "\n"; my %h; open FA, "file_a.txt"; for(){chomp; $h{$_} |= 1}; close FA; open FB, "file_b.txt"; for(){chomp; $h{$_} |= 2}; close FB; my @keys = sort keys %h; print"Only File a:"; for (@keys) {print if $h{$_} == 1}; print "\nOnly in File b:"; for (@keys) {print if $h{$_} == 2}; print "\nIn both files:"; for(@keys){print if $h{$_} == 3};