Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I merge two CSV files from command line?

Tags:

merge

unix

csv

I have two CSV files with the same headers, called a.csv and b.csv. How can I merge the two files into a third c.csv, such that c is composed of all the rows from a and b?

like image 953
NumenorForLife Avatar asked Apr 07 '15 02:04

NumenorForLife


2 Answers

Here is my answer (ignore first row & add nextline)

 cat sample1.csv <(echo) <(tail +2 sample2.csv) > sample3.csv
like image 119
han shih Avatar answered Oct 04 '22 14:10

han shih


A basic merge would be

 cat a.csv <(tail +2 b.csv) > c.csv

This will put all of b.csvafter a.csv.

Edit I've added the <(tail +2 b.csv). It will skip the header in the b.csv file.

edit2

$ cat a.csv
hdr
a
b
c
$ cat b.csv
hdr
e
f
g

$ cat a.csv <(tail +2 b.csv)
hdr
a
b
c
e
f
g

IHTH

like image 25
shellter Avatar answered Oct 04 '22 13:10

shellter