use strict; use warnings; open( my $fh, '<', 'input.txt') or die "Error opening file - $!\n"; open OUT, ">", "output.txt" or die "could not open output.txt $! \n"; my $this_line = ""; my $do_next = 0; my $data = ; while(<$fh>) { my $last_line = $this_line; $this_line = $_; chomp $data; if ($this_line =~ /\Q$data/) { print OUT $last_line unless $do_next; print OUT $this_line; $do_next = 1; } else { print OUT $this_line if $do_next; $last_line = ""; $do_next = 0; } } close ($fh); __DATA__ 4386_7#8 4350_7#6 4414_1#6