#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11151901 use warnings; use Path::Tiny; local $_ = path('FILE1')->slurp . path('FILE2')->slurp; my %seen; while( /^(.*:)(.*)/gm ) { $seen{$1}{$_}++ and print "$1$_\n" for split /~~/, $2; }