Problem adding into an array field!!!

Hi,

Kindly assist by analyzing the code below and suggest changes to achieve the required output.

The input file:
01-010241800000 35000 MV010 02/03/09 0306 03060226 03
02-004103300000 470000 MV010 02/03/09 0301 03010276 03

The objective is to convert field No4. from dd/mm/yy to yyyymmdd

So then I pass it on to:

awk 'BEGIN {OFS=" " }

{ split($4,arr,"/");
$4=sprintf("%d%s%s",arr[2]+2000,arr[1],arr[0])
print $0} '

The output comes as:
01-010241800000 35000 MV010 200302 0306 03060226 03
02-004103300000 470000 MV010 200302 0301 03010276 03

I'm not too sure if the addition "arr[2] + 2000" is taking place. If so, sprintf, truncates the 2009 and instead returns a value of 20 for arr[2]. I guess arr[2] should be of length 4 as opposed to it's original length of 2. Would someone kindly help decipher why I am losing the "09" on arr[2]?

Much Regards

Pawee.

try this...

$4=sprintf("%d%s%s",arr[3]+2000,arr[2],arr[1])

Thanks,Looks so simple, just changing the arrays to begin from 0 to 1....mmmmh!

Appreciated!:b:

Can you please explain the command..

$4=sprintf("%d%s%s",arr[3]+2000,arr[2],arr[1])

thanks in advance

Can you please explain the command..

$4=sprintf("%d%s%s",arr[3]+2000,arr[2],arr[1])

thanks in advance

sprintf is a function to print formatted output. The % values in quotes represent the field arguments that follows & separated by commas-",". The %values indicate the field types i.e %d-decimal, %s-string. The fields have already been split into arrays and thus the new field $4 is given the new format and the acquired types. Hence from 02/03/09 (arr[1]/arr[2]/arr[3])which is our field 4 in the input is rearranged into arrays and formatted to print them in the required order. Our arr[3] should acquire the prefix 20, so we add 2000 to the value 09, and that's why it is a decimal value-for operation "+" purposes.
Regards,

Pawee

Highly Appreciated.:b: