在排除第一列时粘贴多个文件

gae*_*cia 5 unix bash shell

我有一个目录,其中包含100个相同格式的文件:

> S43.txt

Gene    S43-A1   S43-A10  S43-A11  S43-A12
DDX11L1 0       0       0       0 
WASH7P  0       0       0       0
C1orf86 0       15      0       1 



> S44.txt

Gene    S44-A1   S44-A10  S44-A11  S44-A12
DDX11L1 0       0       0       0 
WASH7P  0       0       0       0
C1orf86 0       15      0       1 
Run Code Online (Sandbox Code Playgroud)

我想创建一个包含所有文件中所有列的巨型表,但是当我这样做时:

paste S88.txt S89.txt | column -d '\t' >test.merge
Run Code Online (Sandbox Code Playgroud)

当然,该文件包含两'Gene'列.

  1. 如何一次粘贴目录中的所有文件?

  2. 如何在第一列之后从所有文件中排除第一列?

谢谢.

anu*_*ava 5

如果您正在使用bash,则可以在以下位置使用进程替换paste

paste S43.txt <(cut -d ' ' -f2- S44.txt) | column -t
Gene     S43-A1  S43-A10  S43-A11  S43-A12  S44-A1  S44-A10  S44-A11  S44-A12
DDX11L1  0       0        0        0        0       0        0        0
WASH7P   0       0        0        0        0       0        0        0
C1orf86  0       15       0        1        0       15       0        1
Run Code Online (Sandbox Code Playgroud)

(cut -d$'\t' -f2- S44.txt)将读取S44.txt文件中除第一列之外的所有内容。

要对所有匹配的文件执行此操作S*.txt,请使用以下代码片段:

arr=(S*txt)
file="${arr[1]}"

for f in "${arr[@]:1}"; do
   paste "$file" <(cut -d$'\t' -f2- "$f") > _file.tmp && mv _file.tmp file.tmp
   file=file.tmp
done

# Clean up final output:
column -t file.tmp
Run Code Online (Sandbox Code Playgroud)


gle*_*man 4

join与 --nocheck-order 选项一起使用:

join --nocheck-order S43.txt S44.txt | column -t
Run Code Online (Sandbox Code Playgroud)

column -t让它变得漂亮的命令)

然而,正如你所说,你想加入所有文件,而加入一次只需要 2 个文件,你应该能够做到这一点(假设你的 shell 是 bash):

tmp=$(mktemp)
files=(*.txt)

cp "${files[0]}" result.file
for file in "${files[@]:1}"; do
    join --nocheck-order result.file "$file" | column -t > "$tmp" && mv "$tmp" result.file
done
Run Code Online (Sandbox Code Playgroud)