This is a nifty file, eh? NP Some U and some other Pu 32 40 1 30 20 123.1 -120 And some other stuff #### use IO::File; my $file = IO::File->new; $file->open('< data.dat') or die("Can't read the source:$!"); until ($file->eof) { my $line = $file->getline(); # the regex below will find lines that start with 'NU ' # and have the other fields you want somewhere, surrounded # with spaces. YMMV. if ($line =~ /^NU \s .* \s U \s .* \s Pu \s/sx ) { # we want to get the values from the next line # first, we find the column indexes we want... my @col = split(qr/\s/s, $line); #split on whitespace my %index; for (0..@col-1) { $index{$1} = $_ if $col[$_] =~ /^(NU|U|Pu)$/; } # now we get the next line and split it into columns $line = $file->getline(); chomp($line); @col = split(qr/\s/s, $line); # we can safely reuse @col # now print the appropriate values using the indexes we captured. foreach (keys %index) { printf "%3s = '%s'\n", $_, $col[$index{$_}]; } } # end of if } # end of until