#!/bin/sh perl -i~ -e ' use strict; use warnings; my %excludehash; { local($/) = ""; { while(<>) { while (m/^OBSPOR: (.*)$/mg) { $excludehash{ $1 } = $1 } last if (eof) } } seek(ARGV, 0, 0); { while (<>) { m/^PORNUM:\s+(.*)$/m; print unless exists $excludehash{ $1 } } } }' "datafile"