#!/usr/bin/perl use warnings; use strict; open IN, "input.txt" or die; open OUT, ">output.out" or die; my $sHistory = ''; my $bInHistory = 0; while (my $line=) { if ($line =~ /^MEDICAL HISTORY:(.*)$/) { $bInHistory=1; $sHistory = $1; } elsif ($line =~ /^[A-Z]/) { $bInHistory=0; print OUT $sHistory if $sHistory; } elsif ($bInHistory) { $sHistory .= $line; } } print OUT $sHistory if $bInHistory;