2

I need to merge two CSV files: datasorted1.csv and datasorted2.csv, but my headers keep getting sorted.

head -n 1 datasorted1.csv datasorted2.csv > resultado.csv && tail -n +2 datasorted1.csv datasorted2.csv | sort -t "|" -k 1 >> resultado.csv

Sample data:

Name|Email|Country|Company|Phone
Brent Trujillo|[email protected]|Burkina Faso|Donec LLC|(612) 943-0167
Anthon
  • 78,313
  • 42
  • 165
  • 222
Frank Wilson
  • 79
  • 1
  • 2
  • 11
  • possible double posted question of [Sorting a csv file](http://unix.stackexchange.com/questions/170600/sorting-a-csv-file) – αғsнιη Nov 30 '14 at 06:23

1 Answers1

4

First, read the header from one of the files. Then read the data from both and sort:

head -n 1 sample1.csv > results.csv && tail -q -n +2 sample1.csv sample2.csv | sort -t "|" -k 1 >> results.csv
Jason Hobbs
  • 140
  • 1
  • 4
  • The cmd worked, but now the header is gone. The two headers rows are gone. This is what run: head -n 1 datasorted1.csv && tail -q -n +2 datasorted1.csv datasorted2.csv | sort -t"|" -k 1,1 > resultado.csv – Frank Wilson Nov 30 '14 at 02:26
  • You're not writing the first 'head' to the file, and your second redirect should append to the file. You should run: head -n 1 datasorted1.csv > resultado.csv && tail -q -n +2 datasorted1.csv datasorted2.csv | sort -t"|" -k 1,1 >> resultado.csv – Jason Hobbs Nov 30 '14 at 03:33
  • Bingo man!!!!!!!!!!!! Thanks for the help, as you can tell I'm a rookie, lol. Thanks Jason. – Frank Wilson Nov 30 '14 at 03:55