text - Mixing several files column by column in bash -


i merge 4 .txt files in unique file. however, idea not simple concatenation, otherwise 'interlacement' between input files file1 first 3 columns , files 2-4 must pasted column column in subsequent order. have:

file1:

file1 <- '  ax-1   1    125                 ax-2   2    456             ax-3   3    3445' file1 <- read.table(text=file1, header=f) write.table(file1, "file1.txt", col.names=f, row.names=f, quote=f)  

file2:

file2 <- '  ax-1   aa  ab  aa                 ax-2   aa  aa  ab             ax-3   bb  na  ab' file2 <- read.table(text=file2, header=f) write.table(file2, "file2.txt", col.names=f, row.names=f, quote=f) 

file3:

file3 <- '  ax-1   0.20  -0.89  0.005                 ax-2   0  -0.56  -0.003             ax-3   1.2  0.002  0.005' file3 <- read.table(text=file3, header=f) write.table(file3, "file3.txt", col.names=f, row.names=f, quote=f) 

file4:

file4 <- '  ax-1   1  0  0.56                 ax-2   0  0.56  0             ax-3   1  0  0.55' file4 <- read.table(text=file34, header=f) write.table(file4, "file4.txt", col.names=f, row.names=f, quote=f) 

where expected out file like:

out <- 'ax-1   1    125  aa  0.2  1 ab -0.89 0 aa 0.005 0.56         ax-2   2    456  aa  0   0 aa -0.56 0.56 ab -0.003 0         ax-3   3    3445  bb  1.2  1 na  0.002 0 aa 0.005 0.55' out <- read.table(text=out, header=f) write.table(out, "out.txt", col.names=f, row.names=f, quote=f) 

thus, in out: column 1-3 file1, columns 4,7 , 10 came file2, columns 5,8 , 11 came file3 , columns 6,9 , 12 came file4.

i have idea how in r, original files large , take lot of time. grateful if has idea how perform directly in bash.

this should work:

$ join a1 a2 | join - a3 | join - a4 | awk '{printf "%s %s %s %s %s %s %s %s %s %s %s %s\n", $1, $2, $3, $4, $7, $10, $5, $8, $11, $6, $9, $12}' ax-1 1 125 aa 0.20 1 ab -0.89 0 aa 0.005 0.56 ax-2 2 456 aa 0 0 aa -0.56 0.56 ab -0.003 0 ax-3 3 3445 bb 1.2 1 na 0.002 0 ab 0.005 0.55 

Comments