#!/usr/bin/perl use warnings; use strict; my $file1=<<END; "A123","valueA1","valueB1" "B234","valueA2","valueB2" "C345","valueA3","valueB3" "D456","valueA4","valueB4" "E567","valueA5","valueB5" "F678","valueA6","valueB6" END my $file2=<<END; "C345","valueX1","valueY1" "D456","valueX2","valueY2" END my %hash; # read file1 into a memory hash table open (FILE1, '<', \$file1) or die "$!"; while (my $line = <FILE1>) { chomp $line; my ($key,@vals) = split ',',$line; $hash{$key} = [@vals]; } # process file2, modify the memory hash table open (FILE2, '<', \$file2) or die "$!"; while (my $line = <FILE2>) { chomp $line; my ($key,$val1) = split ',',$line; $hash{$key}[0] = $val1 if $hash{$key}; } # dump the hash table foreach my $key (sort keys %hash) { print "$key,", join(",",@{$hash{$key}}),"\n"; } __END__ Prints: "A123","valueA1","valueB1" "B234","valueA2","valueB2" "C345","valueX1","valueB3" "D456","valueX2","valueB4" "E567","valueA5","valueB5" "F678","valueA6","valueB6"
In reply to Re: Replace value in the text file
by Marshall
in thread Replace value in the text file
by mhoang
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |