$file = $ARGV[0] or die "Missing CSV file on the command line\n"; open($text, '<:encoding(UTF-8)', $file) or die "Could not open '$file' $!\n"; @fields = (); # initialize @fields to be empty while ($line = <$text>) { chomp($line); # remove the newline at the end of the line while ($line =~ m/"([^"\\]*(\\.[^"\\]*)*)",?|([^,]+),?|,/g) { push(@fields, defined($1) ? $1 : $3); # add the matched field } # push(@fields, undef) if $line =~ m/,?/; # account for an empty last field } foreach $field(0..$#fields) { print $field + 1 . " $fields[$field]\n"; } close $file;