Malgi Malgi - 1 month ago 10
Linux Question

How to concatenate multiple files with same header? Some of the files only have header

I have a huge number of files named 0.file.csv,.., 1000.file.csv. I need to concatenate the files, only keep the header of the first file, and get rid of the headers of the rest of the files. The solution I came up with is:

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


But, this solution does not work if some of the files only have header.

The sample input is:

0.file.csv
person_id, dob, year, subject, degree
0,1984/12/01,2014,math,ms

1.file.csv
person_id, dob, year, subject, degree

2.file.csv
person_id, dob, year, subject, degree
200,1990/03/12,2015,physics,bs
201,1991/04/18,2015,math,ms


The output should be:

person_id, dob, year, subject, degree
0,1984/12/01,2014,math,ms
200,1990/03/12,2015,physics,bs
201,1991/04/18,2015,math,ms

Answer

A simpler awk command:

awk 'FNR>1 || NR==1' {0..1000}.file.csv

But this does exactly the same thing as your original (but without the reliance on next). It produces the expected output, but I don't see why your original doesn't. (It did when I tried it.)