while ( ) { print $1 if /^--from\s*([^@\s]+)/; } or: print $1 if /^--from\s*([^@\s]+)/ while ;