With well formatted input, this sort of task is fairly simple. However, as toolic points out, your lack of formatting makes this unnecessarily difficult. If I assume your files are:

file1.txt:

SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS RBCN 78112T107 JT TXN 882508104 KATS STM 861012102 KATS

file2.txt:

AT AU AU AU AV AT BB BE BS BR BSE HU BZ BR CC CL CD CZ CG CN

and I assume the mappings of 1st to 2nd column in file 2 are many to one(so I can use a hash), then you'll likely want something like this:

#!/usr/bin/perl use strict; use warnings; my %mapping; open my $key_handle, '<', 'file2.txt' or die "Open fail: $!"; while (<$key_handle>) { my ($key, $value) = split; $mapping{$key} = $value; } open my $data_handle, '<', 'file1.txt' or die "Open fail: $!"; open my $out_handle, '>', 'file3.txt' or die "Open fail: $!"; while (<$data_handle>) { my @columns = split; print $out_handle "$columns[0]\t$columns[1]\t$mapping{$columns[2]} +\n" }

However, my guess at your file 1 contains the string 'KATS' multiple times in what I guess was column 3 and never appears in your file 2. Given the distribution of 'KATS' in your specified file 1, this mapping is impossible unless file 1 contains only one line.


In reply to Re: Matching column in different files to create a third one by kennethk
in thread Matching column in different files to create a third one by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.