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:
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:
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
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)
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...
...
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
Hey ,
i have this script and i have these loops so it can find a match between 2 arrays :
ARRAY_1=(one two three)
ARRAY_2=(A B C)
VAR='B'
for NUMBERS in "${ARRAY_1}"
do
for LETTERS in "${ARRAY_2}"
do
if ];then
VAR='LETTERS'
... (2 Replies)
Can someone please help me to learn how to deal with loops, arrays and grep?
I have two arrays (lets say I and j) each in a separate file
And have file with lines of data I need to extract, such as
Ruby Smith: some text here
Ruby Smith: some other text here
Ruby Brown: some text here
Ruby... (10 Replies)
if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
My script that I have gotten much help with on here is almost complete..
I need to add a loop, to try a script 4 times, after the 4th time, just stop..
do
auth=$(ps -ef | grep uth | awk '{ print $2}')
if
then
echo "Process is running"
ps=$(ps -ef | grep ps | awk... (3 Replies)
PHP question...
I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names.
So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc
So what I want to do is display the... (3 Replies)
hi guys,
i have an array called ARRAY which has elements in it... i am trying to assign elements of ARRAY to master_array..
i get a =: command not found error..
i=0
while
do
${master_array}=${ARRAY}
((i++))
done
is there something i am missing? (4 Replies)
Hi,
I am trying to write a script which will loop until a certain action has been performed. I have two files i would like to compares.
For example:
file1 has a list of user ids (about 900) from the company's e-mail server.
file2 has a list of user ids (about 50 or so) from... (7 Replies)
im new to UNIX but i know some commands and i bought a book about it but what i was wondering is.. should i learn about it i don't have a job where i use it im still in high school so there isn't any classes i can take where i need to learn it i just wanted to see how it works. From what i've heard... (1 Reply)
can someone give me the link to any site that deals on HP-UX system administration?? Please do not refer me to buy or borrow any books, i just need a free site to learn some quick info from (1 Reply)