Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine multiple csv files into a single excel sheet using unix shell scripting?

E.g. we have 3 csv files 1.csv, 2.csv,3.csv. I want the output as All.xls

containing

the 3 csv files 1.csv, 2.csv and 3.csv in their respective tab.

Could you help me? Thank you in advance.

like image 942
Amit Avatar asked Mar 08 '18 06:03

Amit


4 Answers

Yes the are multiple ways to do what you want. Perl, Python and Ruby have the appropriate modules. Probably other scripting languages also. Depends on which scripting language you are comfortable with.

Here is a pointer to one way of doing what you want using Python: Python script to convert CSV files to Excel

like image 142
Vaibhav Gehlot Avatar answered Oct 05 '22 17:10

Vaibhav Gehlot


The command to cat files together to produce a new file is cat. However, if you simply did a

 cat *csv >All.xls

you would also have header lines in the middle of the resulting files. There are two ways to work around this problem:

The first involves that you create temporary files out of each csv file, where the header line is missing, before putting together the pieces. This can be done using the tail command, for example

tail -n +2 2.csv >2_without_header.csv

The second possibility may or may not be applicable in your case. If - as it is often the case with CSV files - the order of the lines doesn't matter and duplicate lines can be ignored and - as it is likely in your case - the headers are identical, you could simply do a

sort -u *csv >All.xls
like image 40
user1934428 Avatar answered Oct 05 '22 15:10

user1934428


You can try awk '!a[$0]++' ./*.csv > ./all.xls This command will combine all the csv files in current folder and create a new file: all.xls with single header row.

like image 22
Karthikayan Selvaraj Avatar answered Oct 05 '22 17:10

Karthikayan Selvaraj


You can use this well maintained Perl script:

https://metacpan.org/release/Text-CSV_XS

which exists in most Linux distributions (although with different names):

https://repology.org/project/perl:text-csv-xs/versions

Here is the syntax

csv2xlsx -o ~/All.xlsx 1.csv 2.csv 3.csv

like image 23
Cyril Chaboisseau Avatar answered Oct 05 '22 15:10

Cyril Chaboisseau