#! perl -slw use strict; my @data; while( ) { if( /^\s/ ) { push @{ $data[ -1 ] }, split; } else { push @data, [ split ]; } } print "@$_" for @data; =output __DATA__ a1 b1 c1 d1 e1 f1 a2 b2 c2 d2 e2 f2 a3 b3 c3 d3 e3 f3 p3 q3 r3 s3 t3 u3 a4 b4 c4 d4 e4 f4 a5 b5 c5 d5 e5 f5 p5 q5 r5 s5 t5 u5 a6 b6 c6 d6 e6 f6