Question about working with data to create new column

Hello,

I am having a problem with the script I am using to create a column from two columns I have in my file. I am needing to take column 5 and subtract it from column 2 to create column 6. I have included the script I am using and the rawdata I am using.

Raw Data File:
20070701,000004627,encode,29898,13620

Script

####usage: ./date_comp.sh >>outputfilename
awk '{$6=$2-$5
len="%0"length($2)"d"
$6=sprintf(len,$6)
$1=$1
print
}' "file_name"

Thanks in Advance.

nawk -f scott.awk myFile

scott.awk:

BEGIN {
  FS=OFS=","
}

  {
     format="%0" length($2) "d"
     $(NF+1)=sprintf(format, $2 - $5)
     print
  }

vgersh99,

Thanks for the swift reply. I appreciate your assistance. I hate being very rusty and trying to relearn something. LOL Question would that work also for doing a multiple series of math? Meaning I have a column that I want to do math upon. I am taking the column and multiplying it by 0.125, then that total will be subtracted by 1, then that total will be multiplied by 0.0225 for the final total.

((Column*0.125)-1)*0.0225=new column

Meaning would I be able to do something like this:

BEGIN {
FS=OFS=","
}

{
format="%0" length($2) "d"
$(NF+1)=sprintf(format, ((($4*0.125)-1)*0.0225))
print
}

yes, it would - with the slight 'possible' modification. As you're dealing with the floating numbers for your multiplications, you'd probably want to display the 'final total' using floating notation:

format="%0" length($2) "f"

try it out - see what happens.

Thanks Again. That was the ticket. I appreciate the help.