#!/usr/bin/perl use strict; use warnings; my $element = "coucou"; open (INFILE, "){ if ($line =~ /$element/){ my $pre=$`;###to get the previous text from the matching text my $post=$';###to get the following text from the matching text print "PREVIOUS:$pre\nFOLLOWING:$post\n"; } } close INFILE;