Paste side by side multiple files by numerical order

11,406

Solution 1

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

Solution 2

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.

Solution 3

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*
Share:
11,406

Related videos on Youtube

user1687130
Author by

user1687130

Updated on September 26, 2022

Comments

  • user1687130
    user1687130 over 1 year

    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.

  • Stephane Chazelas
    Stephane Chazelas almost 11 years
    bash or zsh or ksh93. That syntax originated in zsh.