open TMP, 'file.txt'; while( ){ chomp; next if /^Total/ or /^\d/; s/\s+//; if (-d and m!/$!){ my $dir = (split !/!)[-1]; print "/$dir/=/$dir\n"; } } close TMP;