我的.tsv
文件位于 100 多个目录中。我想创建一个文件,其中包含tsv
100 个目录中所有文件所需的所有信息。
例如:
Data
|___ SOB33D
|___ SOB33D.tsv
|___ SOB43E
|___ SOB43E.tsv
|___ SOB58D
|___ SOB58D.tsv
|___ SOB113A
|___ SOB113A.tsv
中的数据SOB33D.tsv
如下所示:
target_id length eff_length est_counts
ENST00000456328.2 1657 1525.05 0
ENST00000450305.2 632 500.105 0
ENST00000488147.1 1351 1219.05 0.492522
ENST00000619216.1 68 12.9174 0.70395
ENST00000473358.1 712 580.105 0
ENST00000469289.1 535 403.105 0
SOB43E.tsv
:
target_id length eff_length est_counts
ENST00000456328.2 1657 1525.05 0.174591
ENST00000450305.2 632 500.105 0
ENST00000488147.1 1351 1219.05 7.70424
ENST00000619216.1 68 12.9174 0.295008
ENST00000473358.1 712 580.105 0
ENST00000469289.1 535 403.105 0
SOB58D.tsv
:
target_id length eff_length est_counts
ENST00000456328.2 1657 1525.05 0.282655
ENST00000450305.2 632 500.105 0
ENST00000488147.1 1351 1219.05 2.64778
ENST00000619216.1 68 12.9174 0
ENST00000473358.1 712 580.105 0
ENST00000469289.1 535 403.105 0
SOB113A.tsv
:
target_id length eff_length est_counts
ENST00000456328.2 1657 1525.05 0.0225974
ENST00000450305.2 632 500.105 0
ENST00000488147.1 1351 1219.05 1.35652
ENST00000619216.1 68 12.9174 0
ENST00000473358.1 712 580.105 0
ENST00000469289.1 535 403.105 0
我正在尝试使用cut
.我这样做几乎是正确的。我想要所有文件中的第一列和第二列都相同,并且4th column which is different in all the files
.所以,我像下面这样使用它:
paste */*.tsv | cut -f 1,2,4,8,12,16 > all_samples.tsv
在上面的命令中,我获取了所有文件中相同的第一列和第二列以及所有文件中的第四列。输出如下所示:
输出:
target_id length est_counts est_counts est_counts est_counts
ENST00000456328.2 1657 0 0.174591 0.282655 0.0225974
ENST00000450305.2 632 0 0 0 0
ENST00000488147.1 1351 0.492522 7.70424 2.64778 1.35652
ENST00000619216.1 68 0.70395 0.295008 0 0
ENST00000473358.1 712 0 0 0 0
ENST00000469289.1 535 0 0 0 0
预期输出:
target_id length SOB33D SOB43E SOB58D SOB113A
ENST00000456328.2 1657 0 0.174591 0.282655 0.0225974
ENST00000450305.2 632 0 0 0 0
ENST00000488147.1 1351 0.492522 7.70424 2.64778 1.35652
ENST00000619216.1 68 0.70395 0.295008 0 0
ENST00000473358.1 712 0 0 0 0
ENST00000469289.1 535 0 0 0 0
对于较少数量的文件,我可以使用paste
,但我有100 files in 100 directories
。那么,如何从所有以文件夹名称作为列名称的.tsv
文件中创建一个文件呢?100 directories
任何帮助表示赞赏。坦克
答案1
$ cat tst.awk
BEGIN {
FS=OFS="\t"
numCols = 2
}
{
if ( FNR == 1 ) {
numCols++
val = FILENAME
sub("/[^/]+$","",val)
sub(".*/","",val)
}
else {
val = $4
}
vals[FNR,1] = $1
vals[FNR,2] = $2
vals[FNR,numCols] = val
}
END {
for (rowNr=1; rowNr<=FNR; rowNr++) {
for (colNr=1; colNr<=numCols; colNr++) {
printf "%s%s", vals[rowNr,colNr], (colNr<numCols ? OFS : ORS)
}
}
}
$ awk -f tst.awk */estimate.tsv
target_id length SOB33D SOB43E
ENST00000456328.2 1657 0 0.174591
ENST00000450305.2 632 0 0
ENST00000488147.1 1351 0.492522 7.70424
ENST00000619216.1 68 0.70395 0.295008
ENST00000473358.1 712 0 0
ENST00000469289.1 535 0 0
上面是使用此输入运行的(所有空格都是制表符):
$ head */estimate.tsv
==> SOB33D/estimate.tsv <==
target_id length eff_length est_counts
ENST00000456328.2 1657 1525.05 0
ENST00000450305.2 632 500.105 0
ENST00000488147.1 1351 1219.05 0.492522
ENST00000619216.1 68 12.9174 0.70395
ENST00000473358.1 712 580.105 0
ENST00000469289.1 535 403.105 0
==> SOB43E/estimate.tsv <==
target_id length eff_length est_counts
ENST00000456328.2 1657 1525.05 0.174591
ENST00000450305.2 632 500.105 0
ENST00000488147.1 1351 1219.05 7.70424
ENST00000619216.1 68 12.9174 0.295008
ENST00000473358.1 712 580.105 0
ENST00000469289.1 535 403.105 0