Need heading in files

Hi All,

The below code is splitting the file "data.csv" into several files based on the"client_code" column.

date_yyyymmdd=$(my_date "" -e"%Y%m%d")
file_format="_$date_yyyymmdd.csv
awk '{print  $2, $3>  DIR"/clients_" $1 file_out}' FS=' *; *' OFS=";" DIR="$TEMPL" file_out="_$file_format" $TEMPL/data.csv
data.csv
client_code;contact_name;product_id 
XYZ;AMAR ;AB123456               
ABC;KIRAN;CB789                        
XYZ;RAJ;CS78890                       
ABC;KAMESH;A33535335                                   
XYZ;SOM ;MD6546474777              
XYZ;GANE ;MS657869933553666747

The splitted files are as follows.

clients_ABC_20150615.csv            
ABC;KIRAN;CB789                                              
ABC;KAMESH;A33535335                                   

clients_XYZ_20150615.csv
XYZ;AMAR ;AB123456                                      
XYZ;RAJ;CS78890                                                          
XYZ;SOM ;MD6546474777              
XYZ;GANE ;MS657869933553666747

clients_client_code_20150615.csv
client_code;contact_name;product_id 

The splitted files are not having this heading client_code;contact_name;product_id

I want client_code;contact_name;product_id this heading
in these clients_ABC_20150615.csv and clients_XYZ_20150615.csv splitted files.

Please help me.

Thanks

Try

awk '
NR==1           {HD = $0; next}
                {FN = DIR"/clients_" $1 file_out
                 if (!(CL[FN]++)) print HD > FN 
                 print > FN}
' FS=' *; *' OFS=";" DIR="$TEMPL" file_out="$file_format" $TEMPL/data.csv

i,

Thansks a lot for your code.

I am extremely Sorry, I have posted wrong files.
Slight change in the split files, split files do not contain first column "client_code".
The files will be.

clients_ABC_20150615.csv            
KIRAN;CB789                                              
KAMESH;A33535335                                   

clients_XYZ_20150615.csv
AMAR ;AB123456                                      
RAJ;CS78890                                                          
SOM ;MD6546474777              
GANE ;MS657869933553666747

clients_client_code_20150615.csv
client_code;contact_name;product_id	

I have tried your code.

This is giving the headings as below.

client_code;contact_name;product_id

The heading should be contact_name;product_id.

This is coming correctly for this file.

clients_client_code_20150615.csv
contact_name;product_id	

The heading should be "contact_name;product_id."

clients_ABC_20150615.csv    
contact_name;product_id        
KIRAN;CB789                                              
KAMESH;A33535335                                   

clients_XYZ_20150615.csv
contact_name;product_id
AMAR ;AB123456                                      
RAJ;CS78890                                                          
SOM ;MD6546474777              
GANE ;MS657869933553666747

Please help me.
Thanks,

Change the print > FN to print $2, $3 > FN , then.
And make the first awk script line NR==1 {HD = $2 OFS $3; next}

Are you sure you want the clients_client_code_20150615.csv file? If not, Remove the ; next .