How can I merge two CSV files from command line?

13,481

Solution 1

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

Solution 2

I wanted to join two CSV files, though the below one-liner didn't work
cat file1.csv <(tail file2.csv) > file.csv

I had to, strangely, tell the tail to skip 0 lines
cat file1.csv <(tail +0 file2.csv) > file.csv

Solution 3

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

 cat sample1.csv <(echo) <(tail +2 sample2.csv) > sample3.csv
Share:
13,481
NumenorForLife
Author by

NumenorForLife

Updated on June 20, 2022

Comments

  • NumenorForLife
    NumenorForLife about 2 years

    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?