in reply to replace columns in csv

If your csv is simple enough, (your fields doesn't have any "," inside), you can match lines with regex. (The properly way to go is using some csv module from cpan (like Tux told))

See perlre and search (?<NAME>pattern) to see the use of %+ at the next example:

#!/usr/bin/perl -w use strict; open my $csv1, "<csv1.csv"; open my $csv2, "<csv2.csv"; open my $csv3, ">csv3.csv"; my %csv1_data; while (<$csv1>) { if (/^(?<first>([^,]*,){3})(?<fields>([^,]*,){3}).*$/) { $csv1_data{$+{first}}=$+{fields}; } } while (<$csv2>) { if (/^(?<first>([^,]*,){3})(?<fields>([^,]*,){3})(?<rest>.*)$/) { if (defined $csv1_data{$+{first}}) { print $csv3 "$+{first}$csv1_data{$+{first}}$+{rest}\n"; } else { print $csv3 $_; } } }