Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to paste columns from multiple files in an orderly way?

I've got a output with different file names: file_1.dat..file_15.dat

I'm using this command:

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

But my output files are sorted in 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 command mentioned, the order of the data columns is 1, 11, 13 ... when I want 1,3,5,7 ...

Thank you very much in advance.

like image 351
Enrique Avatar asked Jun 16 '16 10:06

Enrique


3 Answers

Normalize the filenames first.

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

It replaces _ by _0 in all the files with single-digit numbers (? matches a single character).

like image 83
choroba Avatar answered Nov 01 '22 17:11

choroba


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

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

where -t for de-limiting with _ and -k 2 for second column after de-limiting (which is the numbers column) and -g for numerical sort.

file_1.dat
file_3.dat
file_5.dat
file_7.dat
file_9.dat
file_11.dat
file_13.dat
file_15.dat
like image 1
Inian Avatar answered Nov 01 '22 15:11

Inian


You can normalize the files as pointed out in the comments or use a glob like this:

paste file_?.dat file_??.dat

? will expand to exactly one charcter, 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.

This will however fail if you have no files matching the glob as it will be treated literately.

like image 1
Andreas Louv Avatar answered Nov 01 '22 15:11

Andreas Louv