#!/usr/bin/perl @lines = (); while (<>) { push @lines, $_; } sub count { my $pat = shift; my $count = 0; foreach (@lines) { while (/[ ()]$pat[ ()]/g) { $count++; } } return $count; } $last_count = -1; while (1) { @out = (); $count = 0; LINE: foreach (@lines) { $line = $_; next LINE if (/^\(= (XX\d+) .*\)$/ && count($1) == 1); next LINE if (/^\(= .* (XX\d+)\)$/ && count($1) == 1); next LINE if (/^\(NOT \(= (XX\d+) .*\)\)$/ && count($1) == 1); next LINE if (/^\(NOT \(= .* (XX\d+)\)\)$/ && count($1) == 1); push @out, $line; $count++; } last if ($count == $last_count); $last_count = $count; @lines = @out; } foreach (@out) { print; }