Replacing the last field of a line.

Hi,
I wrote a script which extracts data from 2 tables (joining the tables together) and outputs the fields to a csv file.

the output may look something like

scenario 1: a,b,c,d,1,2,3,4 or
scenario 2: a,b,c,d,,,,

now, in the second scenario, there are some empty fields at the end of the line. Basically what I need to do is insert a "0" in the last field if it comes back empty after the database extraction, and i'm not sure how I can do this.

Any help?

thanks,

One way:
file:

a,b,c,d,,,,
a,b,c,d,1,2,3,4

output

 
a,b,c,d,,,,0
a,b,c,d,1,2,3,4

code

awk ' {
    if(substr($0,length($0))==",")
    {   print $0 "0"	}
    else
    {   print $0 }
    }'  filename 

Another way...

sed 's/,$/,0/' file1 > file2

Thanks a lot...that did the job.