in reply to Re: Get data from a file to search and replace within a second file
in thread Get data from a file to search and replace within a second file

Dear all,

thanks for your suggestions. I will need little bit to "digest" your suggestions.

1. GrandFather: I have hard time telling apart your suggested code form some of the comments. I will try to disentangle the thing and get back to you.

2. I tried the code from almut. It works but it will not distinguish between c1 and c11. In other words when C11 is found d1 is added within the 11. The final result is "c1 d11"

3. I will take a look at toolic suggestion later

Thanks for your help.

  • Comment on Re^2: Get data from a file to search and replace within a second file

Replies are listed 'Best First'.
Re^3: Get data from a file to search and replace within a second file
by toolic (Bishop) on Mar 23, 2010 at 16:41 UTC
    2. I tried the code from almut. It works but it will not distinguish between c1 and c11. In other words when C11 is found d1 is added within the 11. The final result is "c1 d11"
    My solution also does not distinguish between c1 and c11. You can add \b anchors, as GrandFather has (see perlre):
    s/\b$k\b/$data{$k}/g;
      Unless c1 etc. are regular expressions, you'll want to use quotemeta. Here's a simple version that does more or less what you want, I think:
      my ($fa, $fb) = @ARGV; open IN, $fa or die $!; my %h; while (<IN>) { my ($str, $rep) = (split)[2,3]; $h{$str} = $rep; } my $pat = join '|', map { quotemeta $_ } keys %h; $pat = qr/\b($pat)\b/; print STDERR "$pat\n"; open IN, $fb or die $!; while (<IN>) { s/$pat/$1$h{$1}/g; print; }