use strict; use warnings; sub main { my $input = ('D:\Perl\Paul\My_Text.txt'); my $output = 'My_Output.txt'; open(INPUT, $input) or die("$input - Not found \n"); open(OUTPUT, '>'.$output) or die("Can't create $output \n"); while(my $text = ){ if($text =~ /(\bdog\b)/){ # The \b above are word boundaries # Result is stored in variable $1 print OUTPUT "$1\n"; } } close(INPUT); close(OUTPUT); } main();