# simple array problem

**URL:** <https://community.unix.com/t/simple-array-problem/275894>\
**Category:** Shell Programming and Scripting\
**Created:** [October 22, 2010, 7:41am UTC](https://community.unix.com/t/simple-array-problem/275894 "2010-10-22T07:41:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![enes71](https://community.unix.com/letter_avatar/enes71/32/6_449c533f586939b6e5f246632a46421f.png) [@enes71](https://community.unix.com/u/enes71)\
**Post date:** [October 22, 2010, 7:41am UTC](https://community.unix.com/t/simple-array-problem/275894/1 "2010-10-22T07:41:48Z")

</div>

Hello experts,

I need help in my code. I have an input file like this:

```nohighlight
100814 1205 1724127 7451382 -10 00:30:1b:48:92:3a
100814 1206 1724127 7451382 -72 00:30:1b:48:92:3a
100814 1207 1724127 7451382 -72 00:30:1b:48:90:3b
100814 1208 1724127 7451382 -72 00:30:1b:48:92:3a
100814 1209 1724127 7451382 -24 00:30:1b:48:92:3a
100814 1210 1724127 7451382 -41 00:30:1b:48:92:3a

```

...goes on..

I would like to take each field into specific arrays and then I will make analysis of them but even I could not extract them into separate arrays properly. Here I wrote a very simple code but i couldnt get the output I want;

```nohighlight
#!/usr/bin/awk -f
{
t[i++]=$2 #only second and fifth field i want to extract
rssi[i++]=$5
}
END {
        for (i=0;i<=NR-1;i++)
        {
        print t
        print rssi
        }
}

```

Then it gives the output;

```nohighlight
1205

-10
1206

-72
1207

-90
1208

```

which seems correct but it did not give all the values, it stopped somewhere in the middle. In my case; I have more lines in my input than I posted here. For example, I have 40 lines in my actual input, and in the output I got half of them...

I will have to compare each value in rssi array with each other in the future. For example, if there is a change it will give the changed values. In this case, i posted above, it would be like;

```nohighlight
-10
-72
-24
-41

```

only changed values.

Also i would like to do all these things only in one script. Is it possible to do it with awk because i am only familiar with awk and i am a starter.

Any help will be appreciated.  
Thank you so much.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [October 22, 2010, 7:55am UTC](https://community.unix.com/t/simple-array-problem/275894/2 "2010-10-22T07:55:40Z")

</div>

```nohighlight
#!/usr/bin/awk -f
{
   t[FNR]=$2 #only second and fifth field i want to extract
   rssi[FNR]=$5
   fnr=FNR
}
END {
        for (i=1;i<=fnr;i++)
        {
        print t
        print rssi
        }
}

```

---

<div class="post-metadata">

**Author:** ![enes71](https://community.unix.com/letter_avatar/enes71/32/6_449c533f586939b6e5f246632a46421f.png) [@enes71](https://community.unix.com/u/enes71)\
**Post date:** [October 22, 2010, 8:13am UTC](https://community.unix.com/t/simple-array-problem/275894/3 "2010-10-22T08:13:21Z")

</div>

thanks a million, thats very helpful

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/6_449c533f586939b6e5f246632a46421f.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [October 22, 2010, 8:44am UTC](https://community.unix.com/t/simple-array-problem/275894/4 "2010-10-22T08:44:23Z")

</div>

```nohighlight
awk '{print$5}' inputfile | uniq

```

---

<div class="post-metadata">

**Author:** ![danmero](https://community.unix.com/letter_avatar/danmero/32/6_449c533f586939b6e5f246632a46421f.png) [@danmero](https://community.unix.com/u/danmero)\
**Post date:** [October 22, 2010, 8:54am UTC](https://community.unix.com/t/simple-array-problem/275894/5 "2010-10-22T08:54:33Z")

</div>

```plaintext
 awk '{if(NR==1 || $5!=f)print $5}{f=$5}' file

```

What exactly you try to achieve ?

---------- Post updated at 08:54 AM ---------- Previous update was at 08:53 AM ----------

> [@ctsgnb](#):
>
> ```plaintext
> awk '{print$5}' inputfile | uniq
> 
> ```

NoGo , do you know why 😉

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/6_449c533f586939b6e5f246632a46421f.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [October 22, 2010, 8:57am UTC](https://community.unix.com/t/simple-array-problem/275894/6 "2010-10-22T08:57:57Z")

</div>

```nohighlight
# echo "100814 1205 1724127 7451382 -10 00:30:1b:48:92:3a
> 100814 1206 1724127 7451382 -72 00:30:1b:48:92:3a
> 100814 1207 1724127 7451382 -72 00:30:1b:48:90:3b
> 100814 1208 1724127 7451382 -72 00:30:1b:48:92:3a
> 100814 1209 1724127 7451382 -24 00:30:1b:48:92:3a
> 100814 1210 1724127 7451382 -41 00:30:1b:48:92:3a" | awk '{print$5}' | uniq
-10
-72
-24
-41
#

```

If this is the output wanted (according to the given sample), then it does the work doesn't it ?

---

<div class="post-metadata">

**Author:** ![michaelrozar17](https://community.unix.com/user_avatar/community.unix.com/michaelrozar17/32/1975_2.png) [@michaelrozar17](https://community.unix.com/u/michaelrozar17)\
**Post date:** [October 22, 2010, 9:20am UTC](https://community.unix.com/t/simple-array-problem/275894/7 "2010-10-22T09:20:00Z")

</div>

The OP need field 2 and 5 of the file to be printed subsequently as

> [@](#):
>
> 1205  
> -10  
> 1206  
> -72  
> 1207  
> -72  
> 1208  
> -72  
> 1209  
> -24  
> 1210  
> -41

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_449c533f586939b6e5f246632a46421f.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [October 22, 2010, 9:34am UTC](https://community.unix.com/t/simple-array-problem/275894/8 "2010-10-22T09:34:19Z")

</div>

Just to expand on what vgersh99 said.

The offending part of your code is the "i++":

> [@enes71](#):
>
> ```plaintext
> #!/usr/bin/awk -f
> {
> t[i++]=$2 #only second and fifth field i want to extract
> rssi[i++]=$5
> 
> ```

You increase the variable "i" two times for each record, so the $2 of the first line is stored in "t[0]", the $5 of the first line in "rssi[1]", the $2 of the second line in "t[3]", etc..

Summary: it is rather dangerous to use inline arithmetic ("i++", "++i", etc.) when you're not absolutely sure what you do. It would probably suffice to change the first "i++" to "i" to make your code work.

I hope this helps.

bakunin

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/6_449c533f586939b6e5f246632a46421f.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [October 22, 2010, 9:48am UTC](https://community.unix.com/t/simple-array-problem/275894/9 "2010-10-22T09:48:32Z")

</div>

> [@michaelrozar17](#):
>
> The OP need field 2 and 5 of the file to be printed subsequently as

Oh Ok then go for

```plaintext
{ while read a a b b b c ; do echo "$a\n$b" ; done } <inputfile

```

---------- Post updated at 03:48 PM ---------- Previous update was at 03:47 PM ----------

```plaintext
# cat myt
100814 1205 1724127 7451382 -10 00:30:1b:48:92:3a
100814 1206 1724127 7451382 -72 00:30:1b:48:92:3a
100814 1207 1724127 7451382 -72 00:30:1b:48:90:3b
100814 1208 1724127 7451382 -72 00:30:1b:48:92:3a
100814 1209 1724127 7451382 -24 00:30:1b:48:92:3a
100814 1210 1724127 7451382 -41 00:30:1b:48:92:3a
# { while read a a b b b c ; do echo "$a\n$b" ; done } <myt
1205
-10
1206
-72
1207
-72
1208
-72
1209
-24
1210
-41
#

```

---

<div class="post-metadata">

**Author:** ![michaelrozar17](https://community.unix.com/user_avatar/community.unix.com/michaelrozar17/32/1975_2.png) [@michaelrozar17](https://community.unix.com/u/michaelrozar17)\
**Post date:** [October 22, 2010, 9:57am UTC](https://community.unix.com/t/simple-array-problem/275894/10 "2010-10-22T09:57:02Z")

</div>

Thanks for the explanation. I too guessed the same reason, but the solution given does not seems to work.Below is how i changed the code from i++ to i

```nohighlight
#!/usr/bin/awk -f
{
t=$2 ; rssi=$5
#s=i
}
END {
        for (i=0;i<=NR;i++)
        {
        print t
        print rssi
        }
} inputfile

```

It just prints 6 empty lines..

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [October 22, 2010, 10:07am UTC](https://community.unix.com/t/simple-array-problem/275894/11 "2010-10-22T10:07:53Z")

</div>

```nohighlight
t[++i]=$2 ; rssi=$5

```

---

<div class="post-metadata">

**Author:** ![enes71](https://community.unix.com/letter_avatar/enes71/32/6_449c533f586939b6e5f246632a46421f.png) [@enes71](https://community.unix.com/u/enes71)\
**Post date:** [October 22, 2010, 4:16pm UTC](https://community.unix.com/t/simple-array-problem/275894/12 "2010-10-22T16:16:48Z")

</div>

> [@ctsgnb](#):
>
> ```plaintext
> # echo "100814 1205 1724127 7451382 -10 00:30:1b:48:92:3a
> > 100814 1206 1724127 7451382 -72 00:30:1b:48:92:3a
> > 100814 1207 1724127 7451382 -72 00:30:1b:48:90:3b
> > 100814 1208 1724127 7451382 -72 00:30:1b:48:92:3a
> > 100814 1209 1724127 7451382 -24 00:30:1b:48:92:3a
> > 100814 1210 1724127 7451382 -41 00:30:1b:48:92:3a" | awk '{print$5}' | uniq
> -10
> -72
> -24
> -41
> #
> 
> ```
> 
> If this is the output wanted (according to the given sample), then it does the work doesn't it ?

Yes, according to my statement above, you are right. Thanks for your help. However, I still need to make an array for each field because I will need to analyze each field in the future. I am trying to write a more complicated script to extract useful information from each field. I hope I will have something to ask again in this code and get your useful comments and help.

---

<div class="post-metadata">

**Author:** ![enes71](https://community.unix.com/letter_avatar/enes71/32/6_449c533f586939b6e5f246632a46421f.png) [@enes71](https://community.unix.com/u/enes71)\
**Post date:** [October 29, 2010, 11:38am UTC](https://community.unix.com/t/simple-array-problem/275894/13 "2010-10-29T11:38:18Z")

</div>

Hello again experts;

I have been working on my code to make it multitask. I deleted unnecessary fields in my input file, here is the last version;

```nohighlight
1716978 7451870 -60 00:3A:98:B6:E3:B0     
1716978 7451870 -61 00:3A:98:B6:E3:B0     
1716978 7451870 -63 00:3A:98:B6:E3:B0     
1716978 7451870 -73 00:3A:98:B6:E3:B0     
1716978 7451870 -65 00:3A:98:B6:E3:B0     
1716978 7451869 -54 00:3A:98:B6:E3:B0     
1716977 7451868 -52 00:3A:98:B6:E3:B0     
1716978 7451867 -52 00:3A:98:B6:E3:B0     
1716985 7451866 -52 00:3A:98:B6:E3:B0     
1716990 7451865 -52 00:3A:98:B6:E3:B0     
1716978 7451864 -52 00:3A:98:B6:E3:B0     
1716979 7451863 -52 00:3A:98:B6:E3:B0     
1716979 7451864 -52 00:3A:98:B6:E3:B0     
1716978 7451865 -53 00:3A:98:B6:E3:B0     
1716978 7451865 -44 00:3A:98:B6:E3:B0     
1716978 7451866 -44 00:3A:98:B6:E3:B0  

```

Suppose 1st field is X and 2nd field is Y. If I treat them in a 2 dimensional coordinate system, each record has its own point as X and Y. What I would like to do is to compare each points in each record and report if there is a 5 meter change. I would like to do this using awk and arrays. It will be comparison of each value with the previous one and if there is a change, make an array for changed instances.  
Here is my code;

```nohighlight
{
   rssi[FNR]=$3
   mac[FNR]=$4
   pos[FNR]=sqrt(($1^2)+($2^2))
   fnr=FNR
}
END {
        for (i=1;i<=fnr;i++)
        {
                if ((pos-pos[i-1])>5 && (pos-pos[i-1])<-5)
                pos_changed=pos #I would like to assign changed values into different array.      
        }
        for (i=1;i<=fnr;i++)
        print pos_changed #at last, reporting the changes
}

```

Output of this code is nothing actually, blank page and then command window again...  
I think I have a problem in assigning the changed value. I searched a lot in the forum but there is no exact problem, similar but not helpful for my code.

I am looking forward to see your feedbacks again.  
Regards.
