use strict; use warnings; use Data::Dumper; use Text::CSV_XS; my $csv = Text::CSV_XS->new(); # create a new object while () { my $status = $csv->parse($_); # parse a CSV string into fields my @columns = $csv->fields(); # get the parsed fields print Dumper(\@columns); } __DATA__ aa,bb,cc 0,2,3 #### $VAR1 = [ 'aa', 'bb', 'cc' ]; $VAR1 = [ '0', '2', '3' ]; #### use strict; use warnings; my @cols = qw (5 11); while () { chomp; my $row = ''; my $start = 0; for my $cs (@cols) { my $offset = $start; my $length = $cs-$start; $row .= substr($_, $offset, $length) . ','; $start = $cs; } $row .= substr($_, $start); print "$row\n"; } __DATA__ 12345678901234567890 abcdefghijklmnopqrst #### 12345,678901,234567890 abcde,fghijk,lmnopqrst