# Problem adding into an array field!!!

**URL:** <https://community.unix.com/t/problem-adding-into-an-array-field/242265>\
**Category:** Shell Programming and Scripting\
**Created:** [June 11, 2009, 1:17am UTC](https://community.unix.com/t/problem-adding-into-an-array-field/242265 "2009-06-11T01:17:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![talk2pawee](https://community.unix.com/letter_avatar/talk2pawee/32/5_5575768a8748004e209b776fc1b2916d.png) [@talk2pawee](https://community.unix.com/u/talk2pawee)\
**Post date:** [June 11, 2009, 1:17am UTC](https://community.unix.com/t/problem-adding-into-an-array-field/242265/1 "2009-06-11T01:17:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rakeshawasthi](https://community.unix.com/user_avatar/community.unix.com/rakeshawasthi/32/224_2.png) [@rakeshawasthi](https://community.unix.com/u/rakeshawasthi)\
**Post date:** [June 11, 2009, 1:27am UTC](https://community.unix.com/t/problem-adding-into-an-array-field/242265/2 "2009-06-11T01:27:39Z")

</div>

try this...

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

```

---

<div class="post-metadata">

**Author:** ![talk2pawee](https://community.unix.com/letter_avatar/talk2pawee/32/5_5575768a8748004e209b776fc1b2916d.png) [@talk2pawee](https://community.unix.com/u/talk2pawee)\
**Post date:** [June 11, 2009, 1:41am UTC](https://community.unix.com/t/problem-adding-into-an-array-field/242265/3 "2009-06-11T01:41:46Z")

</div>

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

Appreciated!:b:

---

<div class="post-metadata">

**Author:** ![Siddheshk](https://community.unix.com/letter_avatar/siddheshk/32/5_5575768a8748004e209b776fc1b2916d.png) [@Siddheshk](https://community.unix.com/u/Siddheshk)\
**Post date:** [June 11, 2009, 2:04am UTC](https://community.unix.com/t/problem-adding-into-an-array-field/242265/4 "2009-06-11T02:04:54Z")

</div>

Can you please explain the command..

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

thanks in advance

---

<div class="post-metadata">

**Author:** ![talk2pawee](https://community.unix.com/letter_avatar/talk2pawee/32/5_5575768a8748004e209b776fc1b2916d.png) [@talk2pawee](https://community.unix.com/u/talk2pawee)\
**Post date:** [June 11, 2009, 2:36am UTC](https://community.unix.com/t/problem-adding-into-an-array-field/242265/5 "2009-06-11T02:36:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Siddheshk](https://community.unix.com/letter_avatar/siddheshk/32/5_5575768a8748004e209b776fc1b2916d.png) [@Siddheshk](https://community.unix.com/u/Siddheshk)\
**Post date:** [June 11, 2009, 2:39am UTC](https://community.unix.com/t/problem-adding-into-an-array-field/242265/6 "2009-06-11T02:39:54Z")

</div>

Highly Appreciated.:b:
