my(@lines) = ; # read file into list #### # match the string "foo" on "word boundaries" my @line_numers = grep { $lines[$_] =~ /\bfoo\b/ } 0 .. $#lines; #### # match lines containing the single word "foo", # ignoring whitespace my @line_numers = grep { $lines[$_] =~ /^\s*foo\s*$/ } 0 .. $#lines;