The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
It's time to learn Scheme iBot UNIX and Linux RSS News 0 04-03-2008 01:40 AM
Loops within loops bthomas Shell Programming and Scripting 8 04-14-2005 10:09 AM
korn shell "loops & arrays" muzica Shell Programming and Scripting 7 09-23-2004 12:02 PM
should i take the time to learn? hiei UNIX for Dummies Questions & Answers 1 03-30-2004 03:11 PM
While loops bookoo UNIX for Dummies Questions & Answers 5 08-23-2002 08:01 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Mar 2008
Location: NYC
Posts: 42
Stumble this Post!
trying to learn for loops, and arrays at the same time

Ok, I've already completed the task this is for, but now I'm trying to go back and find more eloquent solutions for future reference. I have a report I've generated that is formatted like this:

1033 1
1079 4
1453 5
2205 6
1933 7
461 8
646 9
1655 12
975 13
1289 14

The first number is the number of Cable boxes, the second number is the ID of the modulator that they are hooked up to (I'm sure noone cares, but I like adding back story...). What I want to do is using the second number (the ID) lookup the name of that modulator from a file formated like this:

+-----------------------------------------+ +-------------------------+
| QPSK Name Broadcast_Addr ID | | ID QPSK Name |
+-------------------- --------------- --- + + --- --------------------+
| BERGENQPSK1 10.180.127.255 90 | | 1 SMANHUBBQPSKE02 |
| BERGENQPSK1B 10.182.127.255 238 | | 2 SMANHUBBQPSKE03 |
| BERGENQPSK2 10.180.191.255 109 | | 3 SMANHUBBQPSKE04 |
| BERGENQPSK2B 10.182.191.255 239 | | 4 SMANHUBDQPSKE01 |
| BERGENQPSK3 10.180.255.255 158 | | 5 SMANHUBDQPSKE02 |
| BERGENQPSK3B 10.182.255.255 240 | | 6 SMANHUBCQPSKE03 |
| BERGENQPSK4 10.181.127.255 93 | | 7 SMANHUBCQPSKE04

column 7 being the same ID number referenced in the first file, and column 8 being the name I'm trying to get. At this point all I want is to get a list of just the names in the same order that they are on the report so that I can paste it into an excel spreadsheet and it'll line up.

What I've got so far is:

Code:
#!/usr/bin/bash
#create an array with just the mod ID's
a=(`cat HDBOXREPORT |awk '{print $2}'`)
#for each element of the array, look through the MOD list and and print just the name column
for x in "${a[@]}"
do
listQpsk 999|awk '{print $7,$8}'|grep ^$x
done
which returns :

1 SMANHUBBQPSKE02
10 SMANHUBAQPSKE5
11 SMANHUBAQPSKE6
12 SMANHUBAQPSKE7
13 SMANHUBAQPSKE8
14 NMANHUBGQPSKE03
15 NMANHUBGQPSKE04
16 NMANHUBFQPSKE02
17 NMANHUBFQPSKE04
109 BERGENQPSK2
---etc---

ID's and names, but not in the order of the original file (the HDBOXREPORT) and it has 251 lines of output which I don't understand as the HDBOXREPORT has only 143 lines, and the QPSK list only has 152 lines.

I thought possibly the elements of my array were wrong so I tried running:
Code:
a=( 1 2 3 4 5 6 7 8 9 )
#for each element of the array, look through the MOD list and and print just the name column
for x in "${a[@]}"
do
listQpsk 999|awk '{print $7,$8}'|grep ^$x
done
but this again gave me a list with MOD ID's and names, but not in order, and this time with 147 lines of output.

Like I said, this is my first attempt at setting an array or at using a for loop so I don't really know where to proceed from here. Any advice I'd appreciate.

Oh and as a side not, I'm sure there are all kinds of awesome ways to get the results I want, but I'm trying to build up my fundamental skills at the moment, so if you could point me in the right direction using the path I've already started on, rather than a totally different method that would yield the results I'm looking for I'd appreciate it (not that I wouldn't love to see alternate methods)
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-14-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 170
Stumble this Post!
awk

If I correctly understand your requirement:

HDBOXREPORT

Code:
                                                  
1033 1
1079 4
1453 5
2205 6
1933 7
461 8
646 9
1655 12
975 13
1289 14
QSPK-list

Code:
+-----------------------------------------+ +-------------------------+
| QPSK Name Broadcast_Addr ID | | ID QPSK Name |
+-------------------- --------------- --- + + --- --------------------+
| BERGENQPSK1 10.180.127.255 90 | | 1 SMANHUBBQPSKE02 |
| BERGENQPSK1B 10.182.127.255 238 | | 2 SMANHUBBQPSKE03 |
| BERGENQPSK2 10.180.191.255 109 | | 3 SMANHUBBQPSKE04 |
| BERGENQPSK2B 10.182.191.255 239 | | 4 SMANHUBDQPSKE01 |
| BERGENQPSK3 10.180.255.255 158 | | 5 SMANHUBDQPSKE02 |
| BERGENQPSK3B 10.182.255.255 240 | | 6 SMANHUBCQPSKE03 |
| BERGENQPSK4 10.181.127.255 93 | | 7 SMANHUBCQPSKE04

Code:
awk 'NR==FNR{a[$2]=$2;next}
 $7 in a {print $7, $8}' HDBOXREPORT QSPK-list

Output:

Code:
1 SMANHUBBQPSKE02
4 SMANHUBDQPSKE01
5 SMANHUBDQPSKE02
6 SMANHUBCQPSKE03
7 SMANHUBCQPSKE04
Reply With Quote
  #3 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Mar 2008
Location: NYC
Posts: 42
Stumble this Post!
Thank you so much for your reply, honestly I don't understand it...my awk skills (like all of my skills) are pretty basic. I actually wound up constructing something that does my job for me...in the end it turns out I wasn't matching strictly enough. greping for "^$x" was returning more than just the one result I wanted.

In case anyone cares here is what I eventually used to do the job:
Code:
#!/usr/bin/bash
#create an array with just the mod ID's
a=`cat HDBOXREPORT |awk '{print $2}'`
#for each element of the array, look through the MOD list and and print just the name column
for x in $a;
do
cat qpsklist|nawk -v x=$x '{if (x == $7) print $8}'
done
I'd love to have an explanation of that awk statement though if you have time...
Reply With Quote
  #4 (permalink)  
Old 05-14-2008
rubin's Avatar
Registered User
 

Join Date: Nov 2007
Posts: 170
Stumble this Post!
Quote:
Originally Posted by DeCoTwc View Post
...
I'd love to have an explanation of that awk statement though if you have time...

Section related to file HDBOXREPORT.

Code:
NR==FNR  -->  While the condition NR==FNR is true, 
NR --> Record Number which counts all the lines in both files, 
FNR --> Filename Record Number, which counts the lines in only one file, then restarts numbering when the lines of the second file are being processed,  then continue with the  other statement:
{a[$2]=$2;next} --> build the array a with all the elements from field 2 of the first file HDBOXREPORT while NR==FNR is true,
next --> read all the elements from that file, without continuing any further in the script.
Section related to file QSPK-list.

Code:
$7 in a --> Special test operator of assocciative arrays that checks if elements of field 7 ($7) from file QSPK-list are part of the array a created from file QSPK-list. When this condition is true then:

{print $7, $8} --> simply print the required fields from file QSPK-list.
The order of the files being processed is very important -> HDBOXREPORT QSPK-list
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0