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|tempus@mauris.org|Burkina Faso|Donec LLC|(612) 943-0167
1
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
3
  • 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 2: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 3:33
  • Bingo man!!!!!!!!!!!! Thanks for the help, as you can tell I'm a rookie, lol. Thanks Jason. – Frank Wilson Nov 30 '14 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.