a b c d e #### 1 6 11 2 7 12 3 7 13 4 8 14 5 9 23 #### 1 a 6 11 2 b 7 12 3 c 7 13 4 d 8 14 5 e 9 23 #### #!/bin/perl open(IN,"file1"); open(IN2,"file2"); open (OUT,"> output"); while($str=) { @array=split(/\s+/,$str); while($data=) { @box=split(/\s+/,$data); foreach $item(@array) { splice (@box,2,0,"$array($item)"); print OUT "@box\n"; } } }