my $file = ; chomp($file); if ($file =~ /\w+\.dat/) { open(DATOS, "$file") or die "error message"; while () { push(@lines, $_) if /^\d/; } close DATOS; foreach my $line (@lines) { my @fields = split(/\s+/, $line); print "@fields"; # or whatever else you want to do } }