Using awk or grep with Variables in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk or grep with Variables in a script
# 1  
Old 04-29-2012
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:
Code:
#!/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

Last edited by Scrutinizer; 04-29-2012 at 10:39 AM.. Reason: code tags
# 2  
Old 04-29-2012
# 3  
Old 04-29-2012
Then your program will be called 45 times and so will 45 awks. Better put it all in one awk:
Code:
array=( ... )
gsh list_gras | awk -v list="${array[*]}" 'BEGIN{ split (list, numbers) } $NF in numbers'

--
@chihung, did you see this post?

Last edited by Scrutinizer; 04-29-2012 at 11:14 AM..
# 4  
Old 04-29-2012
@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!!
# 5  
Old 04-29-2012
Hi, then put each number on a separate row in a file and try:
Code:
gsh list_gras | awk 'NR==FNR{numbers[$1];next} $NF in numbers' nr_file -


Last edited by Scrutinizer; 04-29-2012 at 12:12 PM..
# 6  
Old 04-30-2012
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
# 7  
Old 05-01-2012
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?

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

But call the script as:
Code:
./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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sending awk variables into curl in a bash script

Hello experts! I have a file1 with the following format (yr,day, month, hour,minute): 201201132435 201202141210 201304132030 201410100110 ... What i want to do is to assign variables and then use them in the curl command to download the text of each event from a web page. What I have... (6 Replies)
Discussion started by: phaethon
6 Replies

2. UNIX for Dummies Questions & Answers

Use of Variables in a sed/awk script

Hi, After looking at the differents post on this forum, I am convinced that I will benefit from the experience of advanced Unix user on some script I have already done for an aeronautical study. Here is one of them : Step 1 : sed -e "s/??/00/g" Base_Awk.txt > Awk_Cut_00.txt4; sed... (11 Replies)
Discussion started by: Marc_Camoc
11 Replies

3. Shell Programming and Scripting

awk issue expanding variables in ksh script

Hi Guys, I have an issue with awk and variables. I have trawled the internet and forums but can't seem to get the exactt syntax I need. I have tried using awk -v and all sorts of variations but I have hit a brick wall. I have spent a full day on this and am just going round in circles. ... (3 Replies)
Discussion started by: gazza-o
3 Replies

4. Shell Programming and Scripting

awk recieving script variables.

Hi, I have a script which recieves 3 variables. #!/bin/bash Directory=$1 Archive=$2 Log_File_Path=$3 ... ... ... gzip -l *.xml.gz |awk '{print $1"^"$2"^"$4}'|sed '$d' In awk instead of $1 which should be Compressed size its recieving Directory name. How should this be... (1 Reply)
Discussion started by: chetan.c
1 Replies

5. Shell Programming and Scripting

Awk script problem - Variables Causing Issue

can someone please explain to me what i'm doing wrong with this code: WELT=$(awk '(($1 ~ "^${caag}$") || ($2 ~ "^${caag}$"))' /tmp/Compare.TEXT) when run from the command line, it works. but it seems to be having a problem doing the comparison when variables are involved. i tested from... (1 Reply)
Discussion started by: SkySmart
1 Replies

6. Shell Programming and Scripting

Script with awk and grep

Hi .. I am working on a shell script to do following task. I have Input file as follows. I have to find failed or Offline disk and run command # hic -ip 172.124.24.59 getlogicalgrp |grep -B10 diskid against the disk ID which is 1 line above the disk state. I am using grep with -b10 is because... (7 Replies)
Discussion started by: dynamax
7 Replies

7. Shell Programming and Scripting

awk (or other) script that assigns fields from a line to multiple variables

Hey all, Unfortunately I have only basic knowledge of awk and/or scripting. If I have a file with lines that can look similar to this: Name=line1 Arg1=valueA Arg2=valueB Arg3=valueC Name=line2 Arg1=valueD Name=line3 Arg1=valueE Arg3=valueF Name=line4 Arg2=valueG ... (4 Replies)
Discussion started by: Rike255
4 Replies

8. Shell Programming and Scripting

Execution problem with grep script (2 variables)

#!\bin\sh TEST=test.log GREP=\usr\bin\grep $GREP -i 'dog\|cat' ${TEST} Why doesn't grep run at all? (10 Replies)
Discussion started by: jazzaddict
10 Replies

9. Shell Programming and Scripting

awk as grep with variables

i have such awk working fine but how to use variable instead of strings awk '/asdasd.*asda.*asdasd/' file2.txt This is not working: awk '/${a}.*${b}.*${c}/' file2.txt Thanks & regards Peter (7 Replies)
Discussion started by: pp56825
7 Replies

10. Shell Programming and Scripting

passing variables to awk from ksh script

I'm trying to write a ksh script that uses awk, but I want to pass variables to awk. For example (not working): if ];then searchstr=$1 lsof -i | awk '{if($9~/SEARCHSTR/) print $2} SEARCHSTR=$searchstr' else echo "usage: $0 <search string>" fi I tried several options. Is it... (3 Replies)
Discussion started by: rein
3 Replies
Login or Register to Ask a Question