simple array problem

Hello experts,

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

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;

#!/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;

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;

-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.

#!/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
        }
}

thanks a million, thats very helpful

awk '{print$5}' inputfile | uniq
 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 ----------

NoGo , do you know why :wink:

# 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 ?

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

Just to expand on what vgersh99 said.

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

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

Oh Ok then go for

{ 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 ----------

# 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
#

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

#!/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..

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

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.

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;

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;

{
   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.