Beto Beto - 2 days ago 6
Linux Question

How to merge lines from csv files in one single file, excluding the header rows

I have a number of csv files that use the following format:

file_1.csv
line 1 -- header row
line 2 -- header row
line 3 -- data row

file_2.csv
line 1 -- header row
line 2 -- header row
line 3 -- data row
...
file_n.csv
line 1 -- header row
line 2 -- header row
line 3 -- data row


and would like to script something that puts them all in one single file having the 2 header lines copied only once, as follows:

fileMerged.csv
line 1 -- header row
line 2 -- header row
line 3 -- data row from file_1
line 4 -- data row from file_2
...
line n+2 -- data row from file_n


What is the best way to achieve this in a Linux server?

Answer

use awk:

awk 'FNR==NR||FNR>2' file_*.csv > fileMerged.csv
Comments