#! perl -slw use strict; print "Age\tMale\tFemale"; print for map{ ## And strip out the combined total. s[(^\d+\s+)[\d\.]+\s+][$1]; $_; } sort { ## Sort the lines by age local $^W; ## Ignoring non-numeric warnings $a <=> $b } grep{ ## Remove any lines that don't fit the pattern m[^\d+\s] } map{ ## Split lines that fit the two column pattern into two m[(^\d+\s+(?:[\d\.]+\s+){3})(.+$)] } ; __DATA__ Your data here #### P:\test>501023 Age Male Female 0 319.1 303.6 1 316.8 301.2 2 321.6 306.8 ... 86 24.0 72.6 87 19.2 61.6 88 15.2 50.9 89 11.6 40.7