Malgi Malgi - 2 months ago 5x
Linux Question

Linux: how to concatenate CSV files in an order and only keep the header of the first file to be concatenated?

I need to concatenate some csv files that have a number at the end of their name as:

file1.csv, file2.csv, ..., file15.csv

These files have the same header, and I only want to keep the header of the first file.

After reading many posts I found a way to concatenate the files and keep the header, but the order of the concatenated files is not kept:

awk 'FNR==1 && NR!=1{next;}{print}' file$*.csv > file.csv


The wrong order is because wildcards are expanded in lexicographic order, not numeric order. You can get numeric order by using a range expression instead of a wildcard.

awk 'FNR==1 && NR!=1{next;}{print}' file{1..15}.csv > file.csv

Or you could change your naming style to give them leading zeroes, file01.csv, file02.csv, etc.

See also glob with numeric order for other techniques.