while () { push @col, $_ =~ m/(\S+\s+\S+)/ #unless m/SN\b.*|generated\b.*|No\b.*|startpattern.*|endpattern.*/; } open(data, ") { $_ =~ s/^(\S\s+\S)/$1 $col[$i++]/; print OUT2 $_; } #### sample input: 0 1 0 1 1 0 sample to attach: 0.85 0.20 0.65 0.50 0.23 0.48 output: 0 1 0.85 0.20 0 1 0.65 0.50 1 0 0.23 0.48