user3781528 user3781528 - 4 months ago 15
Linux Question

combining multiple text files horizontally

There are multiple tab delimited text files in one directory that contain the same string in the file name: “cnv_data.txt” I would like to combine these files horizontally. This is a part of a Perl script. The number of rows and columns is the same in each file (11 columns and 52 rows). Here is an example of what the desired output would look like:

File0ne_cnv_data.txt
SampleID: sample1
CHROM POS FUNC1.gene
chr1 11174372 MTOR


Filetwo_cnv_data.txt
SampleID: sample2
CHROM POS FUNC1.gene
Chr2 1608066 MYCN



Combined_cnv_data.txt
SampleID: sample1 SampleID: sample2
CHROM POS FUNC1.gene CHROM POS FUNC1.gene
chr1 11174372 MTOR chr1 65310459 JAK1


I’ve tried:

my $cmd23 = `paste *final_cnv_data.txt >> $combined_CNV`; print $cmd23;


and also:

my $cmd23 = `cat *final_cnv_data.txt >> $combined_CNV`; print $cmd23;


But the final file is blank.

Is there another way to accomplish what I'm looking for?

Thanks

Answer

check paste command

paste --help
paste $file1 file2 > $file3

Or if you want to combine all files with a sufix you can do

paste SUFIX* >> my_new_file.txt

EDIT

 #!/usr/bin/perl

 my $cmd23 = `paste /tmp/temp/fichero*`;

 print $cmd23;

PD: If you want to print it do not redirect to a file

Comments