user1687130
user1687130

Reputation: 1881

Paste side by side multiple files by numerical order

I have many files in a directory with similar file names like file1, file2, file3, file4, file5, ..... , file1000. They are of the same dimension, and each one of them has 5 columns and 2000 lines. I want to paste them all together side by side in a numerical order into one large file, so the final large file should have 5000 columns and 2000 lines.

I tried

for x in $(seq 1 1000); do 
paste `echo -n "file$x "` > largefile
done

Instead of writing all file names in the command line, is there a way I can paste those files in a numerical order (file1, file2, file3, file4, file5, ..., file10, file11, ..., file1000)?

for example:

file1

1 1 1 1 1
1 1 1 1 1 
1 1 1 1 1
...

file2

2 2 2 2 2 
2 2 2 2 2
2 2 2 2 2 
....

file 3

3 3 3 3 3 
3 3 3 3 3 
3 3 3 3 3
....

paste file1 file2 file3 .... file 1000 > largefile

largefile

1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
....

Thanks.

Upvotes: 7

Views: 12633

Answers (3)

Stephane Chazelas
Stephane Chazelas

Reputation: 6239

With zsh:

setopt extendedglob
paste -d ' ' file<->(n)

<x-y> is to match positive decimal integer numbers from x to y. x and/or y can be omitted so <-> is any positive decimal integer number. It could also be written [0-9]## (## being the zsh equivalent of regex +).

The (n) is the globbing qualifiers. The n globbing qualifier turns on numeric sorting which sorts on all sequences of decimal digits appearing in the file names.

Upvotes: 2

glenn jackman
glenn jackman

Reputation: 246807

If your current shell is bash: paste -d " " file{1..1000}

Upvotes: 12

clt60
clt60

Reputation: 63912

you need rename the files with leading zeroes, like

paste <(ls -1 file* | sort -te -k2.1n) <(seq -f "file%04g" 1000) | xargs -n2 echo mv

The above is for "dry run" - Remove the echo if you satisfied...

or you can use e.g. perl

ls file* | perl -nlE 'm/file(\d+)/; rename $_, sprintf("file%04d", $1);'

and after you can

paste file*

Upvotes: 2

Related Questions