How to merge data of file 1 to file 2 according to id?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • avipinmenon
    New Member
    • Jan 2011
    • 3

    How to merge data of file 1 to file 2 according to id?

    How to merge data of file 1 to file 2 according to id
    file 1
    user id gene
    123 wert
    124 weetr
    125 qwert

    file 2
    user id gene
    123 wwerrt
    126 qweq
    111 wwqw

    output file should be

    output
    file
    user id gene
    111 wwqw
    123 wert
    124 weetr
    125 qwert
    126 qweq

    Code:
    #!/usr/bin/perl
         
        use strict;
        
         
       # my $source_file1 = 'geno.txt';
        # my $source_file2 = 'pheno.txt';
     #   my $dest_file     =  'Output.txt';
         
        my %hash = ();
    	my $line;
    	open(FINAL,">output.txt");
        open(FILE1,'genotype_features.txt');
            while($line = <FILE1>) 
    {
    	 
    	 my ($GenotypeID,$GenotypeName,$GenotypeUniqueName,$AlleleID,$AlleleName,$AlleleAbbreviation,$AlleleType,$AlleleDisplayType,$GeneorConstructSymbol,$CorrespondingZFINGeneID)= split(/\s+/,$line);
    print $line;
                   $hash{$GenotypeID} ={$GenotypeName,$GenotypeUniqueName,$AlleleID,$AlleleName,$AlleleAbbreviation,$AlleleType,$AlleleDisplayType,$GeneorConstructSymbol,$CorrespondingZFINGeneID};
    	        #print $hash{$GenotypeID};
    
      }
        close(FILE1); 
    
        open ( FILE2, 'geno.ods');     
          open (my $FILE3, '>', $dest_file);
           while( $line = <FILE2>) 
    {
         my ($GenotypeID,$GenotypeName,$StartStageOboID,$EndStageOboID,$Process1OboID,$Process2OboID,$PhenotypeKeywordID,$PhenotypeModifier,$PublicationZFINID,$EnvironmentZFINID)= split(/\t/, $line);
         
      
      # print $line;
                   for(grep/^GenotypeID/,keys%hash)  {
         
                   
                       print "$GenotypeID\t$hash{$GenotypeID}\t$GenotypeName\t$StartStageOboID\t$EndStageOboID\t$Process1OboID\t$Process2OboID\t$PhenotypeKeywordID\t$PhenotypeModifier\t$PublicationZFINID\t$EnvironmentZFINID\n";
         
                      
                                      }
           }
         
        
        # close($FILE3);
        close(FILE2);
    problem is comming in hash{genotypeid} they are not taking elements
    Last edited by Niheel; Jan 31 '11, 03:33 AM. Reason: always include your code in your questions, so you don't waste time going back and forth
  • chorny
    Recognized Expert New Member
    • Jan 2008
    • 80

    #2
    So values from first file take precedence?

    Read second file into hash with id as key, than first file. Sort it numerically and output.

    Comment

    • chorny
      Recognized Expert New Member
      • Jan 2008
      • 80

      #3
      Use a third argument to split to make it split only to two fields.

      Code:
      my ($GenotypeID,$rest)= split(/\s+/,$line,2);

      Comment

      • avipinmenon
        New Member
        • Jan 2011
        • 3

        #4
        hey chorny thanks for reply but i am only getting the coloumns and not the data

        ex:

        file
        userid gene
        111
        123
        124
        125
        126

        The information is not comming do help me please

        Comment

        • avipinmenon
          New Member
          • Jan 2011
          • 3

          #5
          dude i didn't get the answers

          Please do help me

          Comment

          Working...