我想将四个.txt文件合并到一个唯一的文件中。然而,这个想法不是简单的连接,而是输入文件之间的“交错”,其中file1将是前三列,而files 2-4必须按随后的顺序逐列粘贴。因此,我们有:
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)其中,我期望的out文件可能如下所示:
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)因此,在out中:列1-3是file1,列4,7 and 10来自file2,列5,8 and 11来自file3,列6,9 and 12来自file4。
我知道如何在R中做到这一点,但我的原始文件太大了,这将花费很多时间。如果有人知道如何在bash中直接执行它,我将不胜感激。
发布于 2016-01-29 17:46:42
这应该是可行的:
$ 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发布于 2016-01-29 17:53:00
试试这个:
paste file1 file2 file3 file4 | awk '{ print $1 " " $2 " " $3 " " $5 " " $9 " " $13 " " $6 " " $10 " " $14 " " $7 " " $11 " " $15 }'如果你的文件有有序的行,这是有效的,由毛罗建议的join是更好的选择。
https://stackoverflow.com/questions/35080990
复制相似问题