use warnings; use strict; open my $fh, '<', 'text.txt' or die $!; while (<$fh>) { if (/^([^ ]+) ([^ ]+)/) { # Skip this if $1 and $2 weren't populated print "$1 $2\n"; } }