How to insert columns from multiple files in an ordered manner?

I have output with different file names: file_1.dat..file_15.dat

I am using this command:

paste result_*.dat | column -s $'\t' -t >> cache/Final_Evolution.dat 

But my output files are sorted this way, and I don't know why:

 file_1.dat file_11.dat file_13.dat file_15.dat file_3.dat file_5.dat file_7.dat file_9.dat 

So, when I use the specified command, the order of the data columns is 1, 11, 13 ... when I want 1,3,5,7 ...

Thank you in advance.

+5
source share
3 answers

Normalize file names first.

 for f in file_?.dat ; do mv "$f" "${f/_/_0}" done 

It replaces _ with _0 in all files with single digits ( ? Matches one character).

+4
source

If you are looking for a way with sort , you can do it like

 sort -t _ -k 2 -g cache/Final_Evolution.dat 

where -t for de-constraint with _ and -k 2 for the second column after de-constraint (which is a column of numbers) and -g for numerical sorting.

 file_1.dat file_3.dat file_5.dat file_7.dat file_9.dat file_11.dat file_13.dat file_15.dat 
+1
source

You can normalize files, as indicated in the comments, or use such a globe:

 paste file_?.dat file_??.dat 

? will expand to one character, so file_?.dat can only expand to: file_1.dat .. file_9.dat and file_??.dat can only expand to file_10.dat .. file_99.dat .

However, this will happen if you do not have files matching glob, since it will be processed literally.

+1
source

All Articles