views:

92

answers:

2

I want to pre-append some text a a CSV file that is created by MySQL.

Basically I want to add the header information for the rows so when the user opens it in Excel they know what each column is.

Whats the best way to do this. I presume there is some easy linux command that can do this? I can also do it in the PHP script. I'd like to know how to do both just for educational purposes.

General setup:

Debian Etc LAMP website Cron calls a PHP script which creates the csv file every night.

Thanks, Derek

A: 

Instead of pre-pending the header text to the data file, start with file which contains the header and then append the data/CSV file to this header file.

ChrisW
I'm using the MySQL INTO FILE facility.. I don't think it will allow that.
Derek Organ
After you have the data file, append it to a header file? Instead of prepending the header file to the data file? Or concatenate them both, the header file and then the data file, to a third file e.g. by using the cat command?
ChrisW
+2  A: 

You can do for example this:

echo "header information" | cat - data.csv > dataInfo.csv

echo prints the header, cat takes this header from standard input and writes it together with everything from data.csv then into dataInfo.csv

seb