Enrique
Enrique

Reputation: 862

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.

Upvotes: 6

Views: 644

Answers (3)

choroba
choroba

Reputation: 241868

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

Upvotes: 4

Andreas Louv
Andreas Louv

Reputation: 47099

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.

Upvotes: 1

Inian
Inian

Reputation: 85590

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

Upvotes: 1

Related Questions