use strict; use warnings; open FH, '<', "filename.txt" or die "Can not open file $!"; while() { my ($first_word) = $_ =~ /^(\w+)/; if ($first_word eq 'your string') { #Your wish. } }