Insert multiple files side by side in numerical order

I have many files in a directory with similar file names such as file1, file2, file3, file4, file5, ....., file1000. They are the same size, and each of them has 5 columns and 2,000 rows. I want to insert them all together side by side in numerical order into one large file, so the final large file should contain 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 the file names on the command line, is there a way I can insert these files in numerical order (file1, file2, file3, file4, file5, ..., file10, file11, ..., file1000)?

eg:

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 .... 

insert file1 file2 file3 .... file 1000> large file

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.

+6
source share
3 answers

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

+11
source

you need to rename files with leading zeros like

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

The above means dry running - delete echo if you are satisfied ...

or you can use for example. Perl

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

and after

 paste file* 
+2
source

With zsh :

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

<xy> must match positive decimal integers from x to y . x and / or y can be omitted, so <-> is any positive decimal integer. You can also write [0-9]## ( ## as the equivalent of zsh regex + ).

(n) are the classifiers of globes. The n globbing key enables numeric sorting, which sorts by all sequences of decimal digits displayed in file names.

+2
source

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


All Articles