Here's a way to do it if your flatfile consists of one record per line, with whitespace before the "record number" at the end: #!/usr/bin/perl -- -*-fundamental-*- use strict; use Fcntl qw(:flock); ## Set these as needed... my $flatfile = "flat.txt"; my $num2delete = "12"; my $result = &DeleteRecord($flatfile, $num2delete); if ($result) { print "No records were deleted.\n"; } else { print "Deleted record number $num2delete.\n"; } exit; sub DeleteRecord($$) { my $flatfile = shift; my $num2delete = shift; ## Open the flatfile in read/write mode open(FLATFILE, "+< $flatfile") or die "Could not open $flatfile: $!\ +n"; ## Lock it exclusively flock(FLATFILE, LOCK_EX) or die "Could not lock $flatfile: $!\n"; ## Try and find a match my $found=0; while() { last if /\s+$num2delete$/ and ++$found; } ## If no match, we clean up and return a 1 unless ($found) { close(FLATFILE); ## Also unlocks for you return 1; ## Have the caller handle this case; } ## Mark the spot where the record to be deleted starts my $here = tell(FLATFILE) - length $_; ## Read in the rest of the file: my @slurp = ; ## Rewind to the spot we marked earlier seek(FLATFILE,$here,0); ## Stick everything else back in, overwriting the file as we go: print FLATFILE @slurp; ## If we don't truncate, the last record will appear twice (think ab +out it) truncate(FLATFILE, tell); ## Close and unlock, and return success to the caller close(FLATFILE); return 0; }