2015-09-26 15 views
-2

我想按bash按列組合幾個txt文件。每個文件的名稱都是File,後面跟着一個數字。因此,File1.txtFile2.txtFile3.txt等等。下面以3個文件爲例(但我有幾個)。在bash中按列組合文件,條件爲

文件1:

######## infx infx infx 
######## infx infx infx 
####### infx infx 
probeset_id sample1 sample2 sample3 
PR01   1  2  0 
PR02   -1  2  0 
PR03   2  1  1 
PR04   1  2  1 
PR05   2  0  1' 

文件2:

######## infx infx infx 
######## infx infx infx 
probeset_id sample4 sample5 sample6 
PR01   2  2  1 
PR02   2  -1  0 
PR03   2  1  1 
PR04   1  2  1 
PR05   0  0  1' 

文件3:

# The dfn 
######## infx infx infx 
######## infx infx infx 
probeset_id samplen1 samplen2 samplen3 
PR01   2  -1  1 
PR02   1  -1  0 
PR03   2  1  1 
PR04   1  2  -1 
PR05   0  2  1' 

若要完成後續output.txt中:

$ head output.txt 
     probeset_id sample1 sample2 sample3 sample4 sample5 sample6 samplen1 samplen2 samplen3 
    1  PR01  1  2  0  2  2  1  2  -1  1 
    2  PR02  -1  2  0  2  -1  0  1  -1  0 
    3  PR03  2  1  1  2  1  1  2  1  1 
    4  PR04  1  2  1  1  2  1  1  2  -1 
    5  PR05  2  0  1  0  0  1  0  2  1 
ps。

ps。具有##的行數可以在文件之間有所不同。任何想法解決這個問題?

回答

1

您可以使用下一個命令

join <(grep -v "^#" file1) <(grep -v "^#" file2) | 
join - <(grep -v "^#" file3) | awk '{print (NR>1?NR-1:""), $0}' 

 
probeset_id sample1 sample2 sample3 sample4 sample5 sample6 samplen1 samplen2 samplen3 
1 PR01 1 2 0 2 2 1 2 -1 1 
2 PR02 -1 2 0 2 -1 0 1 -1 0 
3 PR03 2 1 1 2 1 1 2 1 1 
4 PR04 1 2 1 1 2 1 1 2 -1 
5 PR05 2 0 1 0 0 1 0 2 1