Insert multiple files excluding the first column

I have a directory with 100 files of the same format:

> 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 

I want to create a giant table containing all columns from all files, but when I do this:

paste S88.txt S89.txt | column -d '\t' >test.merge

Naturally, the file contains two columns 'Gene'.

  • How can I insert ALL files into a directory right away?

  • How can I exclude the first column from all files after the first?

Thanks.

+4
source share
2 answers

use joinwith the -nocheck-order option:

join --nocheck-order S43.txt S44.txt | column -t

(team column -tto make her beautiful)

However, as you say, you want to join all the files, and it only takes 2 to connect, you have to do this (if your shell is 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
+2

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

(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
+3

Source: https://habr.com/ru/post/1627200/


All Articles