simple array problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simple array problem
# 1  
Old 10-22-2010
simple array problem

Hello experts,

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

Code:
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;

Code:
#!/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[i]
        print rssi[i]
        }
}

Then it gives the output;
Code:
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;

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

Last edited by vgersh99; 10-22-2010 at 08:53 AM.. Reason: code tags, please!
# 2  
Old 10-22-2010
Code:
#!/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[i]
        print rssi[i]
        }
}

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 10-22-2010
thanks a million, thats very helpful
# 4  
Old 10-22-2010
Code:
awk '{print$5}' inputfile | uniq

# 5  
Old 10-22-2010
Code:
 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 ----------




Quote:
Originally Posted by ctsgnb
Code:
awk '{print$5}' inputfile | uniq

NoGo , do you know why Smilie
# 6  
Old 10-22-2010
Code:
# 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 ?
# 7  
Old 10-22-2010
The OP need field 2 and 5 of the file to be printed subsequently as
Quote:
1205
-10
1206
-72
1207
-72
1208
-72
1209
-24
1210
-41
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

simple wc problem

Hi there, Im sure there is a simple explanation for this but I have a file like this with no balnk lines peter paul john I run the command # var=`grep paul file.txt` # echo $var paul # echo $var | wc -l 1 but when I grep for a value that isnt in the file, i still... (4 Replies)
Discussion started by: rethink
4 Replies

2. UNIX for Dummies Questions & Answers

LWP::Simple Problem !!

Hi All, I'm having a problem when I run the following code for example perl -e 'use LWP::Simple; getprint "http://google.com"' Can't locate LWP/Simple.pm in @INC (@INC contains: /System/Library/Perl/5.8.6/darwin-thread-multi-2level /System/Library/Perl/5.8.6... (7 Replies)
Discussion started by: pawannoel
7 Replies

3. Shell Programming and Scripting

Please help me with a simple problem

Hi, I have a very simple script like below: for n in 10 20 30 do for a in 30 40 50 60 70 80 do for r in 2 3 4 5 6 7 do m=$((r*a)) count=1 while do echo "a = " $a ", m = " $m ", n = " $n ... (2 Replies)
Discussion started by: Dark2Bright
2 Replies

4. Programming

Simple g++ compilation problem

I'm trying to port some code over to unix Solaris. I'm not really a unix programmer so I'm sure this is something straight-forwards but I'm getting the following: > g++ -c CTrade.cpp In file included from CTrade.cpp:1: stdafx.h:6: warning: `#pragma once' is obsolete CTrade.cpp:4: iostream: No... (1 Reply)
Discussion started by: achartley
1 Replies

5. UNIX for Dummies Questions & Answers

simple fork() problem

I have this little program ... int main(void){ printf("Before"); fork(); printf("After"); } output is this..... BeforeAfterBeforeAfter Shouldnt it be.....BeforeAfterAfter After parent is forked child receives the copy of program and continues from next statement... (3 Replies)
Discussion started by: joker40
3 Replies

6. UNIX for Dummies Questions & Answers

simple way to read an array in ksh

hi, I'm a newbie to shell scripting. I wanted to initialise an array using basic for loop and read it. Then i want to print it as a .CSV file.. Any help would me much appreciated.. (1 Reply)
Discussion started by: pravsripad
1 Replies

7. Shell Programming and Scripting

Simple list file ls to an array

Hi all, Simple question, how can I simply create an array from listing the files in a directory i..e myvar=`ls`; echo $myvar gives a fulllist of files/ directories how can I now convery myvar to an array so I can loop around and read each file/dir? Thnaks CF:) (6 Replies)
Discussion started by: cyberfrog
6 Replies

8. UNIX for Dummies Questions & Answers

Simple Array in Ksh Scripting

Ksh Scripting Can some one give me a simple example of array operations using ksh. For Ex: week_array = {Sunday Monday Tuesday Wednesday Thursday Friday Saturday} I want to assign and retrieve and print them along with their index. I am looking for the o/p like: 0 Sunday 1 Monday ... (2 Replies)
Discussion started by: ravikirankethe
2 Replies

9. Shell Programming and Scripting

simple Bourne problem

Hi, I'm a newer for this languages, and I have a log file, which is something like this: 35.75.253.207 - - "GET /products/orgonizer/title.png HTTP/1.1" 200 1555 "-" "Mozilla 1.4" Now, I want to write a shell code to accoplish like ./XXX.sh -N n n is a number by user input, the code should... (5 Replies)
Discussion started by: pnxi
5 Replies

10. UNIX for Advanced & Expert Users

A simple Samba problem, please help

Hi, I have successfully setup my Samba server on my Sun Solaris 8 Sparc machine. My Win2000 workstation is able to see the unix worstation and its shared directory from Network Neighborhood. However, the Unix workstation appears as Ip address instead of Hostname on my network neighborhood... (5 Replies)
Discussion started by: champion
5 Replies
Login or Register to Ask a Question