Using awk or grep with Variables in a script

Good day Geeks,

Am having an issue with using variables in a rather simple script, the script is as follows:

#!/bin/bash
### Script written by Adigun Gbenga
### Date: April 28, 2012

array=( 1 	2 	3 	4 	5 	29	7 	8 	9 	10	11	35	36	37	38	16	17	18	19	20	21	67	46	47	70	71	185	73	74	480	75	76	77	78	57	58	81	82	83	334	63	64	65	22	88 )

for i in "${array[@]}"
	do
	gsh list_gras | awk '$NF==$i'
done

What i want to do is that i want to filter exactly the numbers using the arrays but bash and awk are in conflict with the $, the output of the script just gives an endless loop. The command works well on the shell without problem. Your help will be highly appreciated.

Also how do go about using a text file as input instead of the array i.e. define the numbers in a separate file and call the file from the script.
P.S-- The command is an Ericsson command (but it runs a full linux box and i have been writing script on the system without a problem)

Thanks

This post - Chi Hung Chan: Four Ways to Pass Shell Variables in AWK may be able to help u

Then your program will be called 45 times and so will 45 awks. Better put it all in one awk:

array=( ... )
gsh list_gras | awk -v list="${array[*]}" 'BEGIN{ split (list, numbers) } $NF in numbers' 

--
@chihung, did you see this post?

@Scrutinizer---thanks man for your response, will try this out.

Please about my second question, how do i use a text file as input instead of the manual entry of the array, this is to make this more flexible.

Thanks for the quick response, i know someday i will get to where the Eagles soar in scripting!!

Hi, then put each number on a separate row in a file and try:

gsh list_gras | awk 'NR==FNR{numbers[$1];next} $NF in numbers' nr_file -

Hey Scrutinizer,

It worked quite well but just some further help, am so sorry for bothering you man:
1) The script is sorting out the output numerically, how do i stop this, it is crucial for it to display the output with respect to the numbers in the arrays.

2) It is not showing errors encountered instead it will just skip to the next without saying that there was an error (but with grep it was still displaying the error), it is crucial for me to know which values the command is having issues with.

3) Also how do set input file as a variable so that i can use any file from the shell as input, see below:
./rac_huawei_script.sh < huawei_bscs

Also man, where did you learn all this? Is there a recommended reading list that can help me out?

Thanks

Hi inifinitydon,

I do not understand 1) . There should be no sorting and the output order of gsh list_grad should be preserved

2) what error do you mean? numbers in the last that do not exist in the numbers file?. What do you want to do with those?

gsh list_gras | awk 'NR==FNR{numbers[$1];next} $NF in numbers' "$1" -

But call the script as:

./rac_huawei_script.sh huawei_bscs

About learning this stuff, I like this O'Reilly book:
sed & awk, 2nd Edition-O'Reilly Media

There is also a solid on-line tutorial:
Awk - A Tutorial and Introduction - by Bruce Barnett

Other ingredients are practice and hanging out on these forums :wink: