dear monks, I have 2 files which has almost common number of columns. the first file has 5 lines, whereas the second file has only 3 lines. in this, a particular field ID in second file will definitely be present in first file. So, if the fields are similar to both the files, the content from second file should be printed. The remaining unique ID's in first file should also be printed. For example: TAT is the ID common to both the files. So, the 6th column in both files has to be compared.
file1.txt: 234 13 4 49 + TAT_01 id_nu1 explan1 236 123 3 67 + TAT_02 id_nu2 explan2 534 12 8 13 + TAT_03 id_nu3 explan3 764 124 9 33 + TAT_04 id_nu4 explan4 224 153 2 37 + TAT_05 id_nu5 explan5 file2.txt: 334 138 34 39 - TAT_02 PAS_1 id_nu2 new2 545 154 83 11 + TAT_03 PAS_2 id_nu3 new3 765 131 21 12 - TAT_05 PAS_3 id_nu5 new5 desired_results: 234 13 4 49 + TAT_01 id_nu1 explan1 334 138 34 39 - TAT_02 PAS_1 id_nu2 new2 545 154 83 11 + TAT_03 PAS_2 id_nu3 new3 764 124 9 33 + TAT_04 id_nu4 explan4 765 131 21 12 - TAT_05 PAS_3 id_nu5 new5
The result file has contents of second file for similar TAT id's and content of first file for different(unique between two files) TAT id's. the program which i have written is not giving me the right answer. Please have a look at it:
#!/usr/bin/perl open(FH1,"file1.txt"); open(FH2,"file2.txt"); @array=<FH2>; $jo=join("",@array); @spl=split("\n",$jo); while($line1=<FH1>){ @hold=(); @coll=split("\t",$line1); #$len=$coll[1]-$coll[2]; #$len=~s/-//g; $hit=$coll[5]; @hold=grep(/$hit/,@array); $held=shift(@hold); if(defined $held){ ($c1,$c2,$c3,$c4,$c5,$c6,$c7,$c8,$c9)=split("\t",$held +); print "$c1\t$c2\t$c3\t$c4\t$c5\t$c6\t$c7\t$c8\t$c9\n"; } else{ print "$coll[0]\t$coll[1]\t$coll[2]\t$coll[3]\t$coll[4 +]\t$coll[5]\t$coll[6]\t$coll[7]\t$coll[8]\n"; } }
please suggest me a better way to solve this problem. Thank you very much !!

In reply to combining two files based on missing values by sugar

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.