$\ = "\n"; # always print a newline at the end while (<>) { @s = split /\s{3,}/; # split the input into phrases print grep /NOTICE/, @s; # print the matching phrases }