while (<>) { print "$1\n" if /\s*(\w+)\s+NS\s+.*/ #print what found in parens if the (current) line # begins whith zero or more space/tab... one or # more letter (we save it by enclosing it in parens) # at least one space then the 2 letters NS followed # by at least a space anf maybe other char }