How can I add a header to a csv file

I have a csv file which has no header. the file has 15 fields and needs to go out with a header of 8 fields.

The header content needs to have some variables and some fixed that i have set up:

variable header fields
OUTFILE_YEAR=`date '+%y'`
DATE=`date '+%d%m%y'`
TIME=`date '+%H:%M:%S'`
NNNN="hello"
RR=`wc -l ../xxxx/"xxxxx.txt"`

fixed header fields
1
1
4

How can i get the above in a header and onto the already existing csv file?:confused:

Assuming that you CSV file only uses commas to seperate the fields ....

#!/usr/bin/ksh93

OUTFILE_YEAR=`date '+%y'`
DATE=`date '+%d%m%y'`
TIME=`date '+%H:%M:%S'`
NNNN="hello"
RR="RRRRRRRR"            # dummy value

HEADER="$OUTFILE_YEAR,$DATE,$TIME,$NNNN,$RR,1,1,4"

cp csvfile /tmp/csvfile.bak
echo $HEADER > outfile
cat csvfile  >> outfile
mv outfile csvfile

If your CSV file is of the format "field1","field2, .... , "fieldN" then the HEADER line shold be changed to

HEADER="\"$OUTFILE_YEAR\",\"$DATE\",\"$TIME\",\"$NNNN\",\"$RR\",\"1\",\"1\",\"4\""

I have set it like this but the fileout is just as it was going in. The header.sh does not get appended to the file:

HEADER="\"xxxx$OUTFILE_YEAR$INCREMENT\",\"xxxxx\",\"$DATE\",\"$TIME\",\"1\",\"4\",\"$RR\""

echo $HEADER > ../$CUSTOMER_DIRECTORY/header.sh
cat ../$CUSTOMER_DIRECTORY/header.sh >> ../$CUSTOMER_DIRECTORY/${FILE_OUT_NAME}${OUTFILE_YEAR}${INCREMENT}.csv

Does anyone know why is this not working?:confused:

The header should be prepended - not appended to the existing csv file.

Try the following

echo $HEADER > new.csv
cat ../$CUSTOMER_DIRECTORY/${FILE_OUT_NAME}${OUTFILE_YEAR}${INCREMENT}.csv >> new.csv
cat new.csv

The first line of new.csv should be the header. The remaining lines should contain the existing csv data,

Hi fpmurphy

i do exactly what you say:

echo $HEADER > ../$CUSTOMER_DIRECTORY/new.csv
cat ../$CUSTOMER_DIRECTORY/${FILE_OUT_NAME}${OUTFILE_YEAR}${INCREMENT}.csv >> ../$CUSTOMER_DIRECTORY/new.csv
cat ../$CUSTOMER_DIRECTORY/new.csv

the output of the above command (new.csv) is:

"xxxx08","xxxxxxx","171008","15:05:39","1","4"," 232 ../xxxx/xxxxx xxxx_080910.txt"

exactly the same as when it goes in??? it just seems to ignore the ../$CUSTOMER_DIRECTORY/${FILE_OUT_NAME}${OUTFILE_YEAR}${INCREMENT}.csv ??? :confused:

see below contents of both files that go in to above command:

contents of new.csv
"xxxx08","xxxxxxx","171008","15:05:39","1","4"," 232 ../xxxx/xxxxx xxxx_080910.txt"

contents of ${FILE_OUT_NAME}${OUTFILE_YEAR}${INCREMENT}.csv"

"","ABC","","12345678","","","","","","","","","","","CH1 7DD"

Why is it not working??

Not that it matters but i am in KSH

That code is the same that I use to add headers and trailers. Verify that you have the input file defined correctly. Echo the file name or do an ls on it. It sounds like it may not be interpreting the variables in the file name correctly.

Sorted the problem I had a command previous to what i was doing here and I was piping from that command into this and the file had not yet been created so it was showing as a blank file.

Works great now, thanks everyone for their help.