Given a set (2 or more) of tab-delimited .txt files in the format of:

File A:
I 9 A B 0.25 6 I 21 B A 1 6 I 33 C C 0.5 2 I 40 D D 1 2 I 56 A E 1 2
File B:
I 9 A B 0.30 8 I 21 B A 1 6 I 33 C C 1 2 I 40 D D 1 5

Is it possible to identify partially duplicate lines based solely on the contents of columns-1-4 and then merge the numerical contents of columns 5-6 into a single line? For example, given the two above .txt files, the desired output would be:

I 9 A B 0.275 14 I 21 B A 1 12 I 33 C C 0.75 4 I 40 D D 1 7 I 56 A E 1 2

i.e. if the contents of columns 1-4 match those of 1 or more other lines, average the numerical value in column 5 between all matched lines (for example on the first line - (0.25+0.3)/2 = 0.275) and sum the contents of column 6 between all matched lines (for example on the first line - 6+8=14). If the content of columns 1-4 is unique, print the line without modification (for example - the final line of File A which has no cognate partner within File B)

How could this be done? My knowledge of Perl is rather limited; I would presume I would need to read all of the lines into a single array and then perhaps use seen or the uniq function found in List::MoreUtils to begin to look for partial duplicate lines, but I am rather unsure. Thank you in advance for any help.

In reply to Merging partially duplicate lines by K_Edw

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.