Paste side by side multiple files by numerical order

柔情痞子 提交于 2019-11-28 04:26:13

问题


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.


回答1:


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




回答2:


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*



回答3:


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.



来源:https://stackoverflow.com/questions/17075070/paste-side-by-side-multiple-files-by-numerical-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!