nbdas has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am new to perl and i am looking for a suitable perl script to solve my problem. I have multiple text files(around 7-8) with unequal number of columns(2k-3k) in each file but equal number of rows(7k) and the first column of each file is same/common.I need a merged output file where the first column should occur once and rest columns of all files be placed side by side. Hoping for the reply asap Thank You
  • Comment on merge multiple files based on a common column and each having 2k-3k columns

Replies are listed 'Best First'.
Re: merge multiple files based on a common column and each having 2k-3k columns
by Corion (Patriarch) on Sep 06, 2018 at 06:55 UTC
      I have run join command of linux as join -j 1 file-1.txt file-2.txt | join -j 1 file-3.txt - | join -j 1 file-4.txt - > merge-output.txt but the output file doesnot open in excel in right way as generally it should have due to large number of columns may be

        Excel has a limit of 16,384 columns. Why do you need to create such a large spreadsheet ? Are there only certain columns in each file that you are really interested in ?

        poj
Re: merge multiple files based on a common column and each having 2k-3k columns
by Laurent_R (Canon) on Sep 06, 2018 at 07:19 UTC
    The typical way to solve such a problem is to use a hash to store the content of the files, with the (first) common column being the hash key and the rest of the lines the hash values (you'll need to concatenate the lines into the hash values). Once you've read all the files one by one and store their contents into the hash, simply output the hash keys and values.

    Update: Fixed typo: s/One/Once/

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: merge multiple files based on a common column and each having 2k-3k columns
by hippo (Archbishop) on Sep 06, 2018 at 08:17 UTC

    You could just run join (from PerlPowerTools) 6-7 times. Or, as a learning experience, you could modify it to accept any number of files as arguments.