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