Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a variable to awk/gawk 2

Status
Not open for further replies.

Wildcatlg

Programmer
Aug 2, 2008
6
US
#!/bin/sh
#Section: 04
#Name: Scott Deese
my_awk="awk -v var=$1 ' /$var/ { print \$2 } ' "

echo $my_awk
schemas=`$my_awk database.txt`

ive tried this many different ways and this was the last way describe by my teacher that doesnt work.
I need to pass a variable from the sh shell to awk. the variable is $1 (first cmd line argument)
he said i sould make the awk script my variable them evaluate it in the sh script.... tried that this is what i come up with

localhost ~]$ sh ir.sh dont
awk -v var=dont ' // { print $2 } '
awk: '
awk: ^ invalid char ''' in expression

i've tried my_awk="awk ' /$1/ { print \$2 } ' "
same thing happens except the shell does put the $1 value in the correct spot
but it is never evaluated because it gives me the same syntax error
I would appreciate any help
 
Code:
#!/bin/sh
#Section: 04
#Name: Scott Deese

[COLOR=red]my_awk=$( awk -v var=$1  '$0 ~ var{ print $2 }' input_here )[/color]

echo $my_awk
schemas=`$my_awk database.txt`         # is $my_awk a command ?
 
Oops ... quote the variables ...

Code:
... -v var="$1" ...
 
$my_awk is a variable script that is defined by the argument im trying to write
and you freaken brilliant. this little piece of code has been driving me crazy for about 2 days.
Ok next quest is can you explain maybe why you have to put it in the $(......) to get it to idetify the line of code as such instead of the normal `......` and what does the $0 charectors do in the awk script and i take it to use a variable as a parameter in the script you just dont use the /.../
I appreciate any understand you can give as im new to this. Thanks
 
Basically to give you an over view. the $my_awk script is something ill use ALOT in my script because it simply reads a file that has 2 columns. Column 1 is the database name. Column 2 is the schema file assosiated with it so now instead of rewritting that script everytime i need the schema file i now just have to type out an awk script to find the right line in schemas.txt to find $my_awk
Now i just have to write the while statement to make sure it isnt blank.
 
OK,the construct $(..) is just another notation of the newer shells` command substitution. I could have very well used the backtikcs `...`, but i chose $() for better readability.

$0 ~ var simply means, if the value of variable var is present in the records of the file, print that record.
AWK doesn't accept the syntax /var/, where var is a variable defined with ...-v var=..., it'll error out, but it accepts an actual pattern found in the file:

Code:
awk '/pattern/' file

# which is the same thing as

awk '$0 ~ /pattern/' file

Well you can use the variable var inside awk as is, but there are two problems, first you need to know shell quoting rules very well, and second, that rule won't work if the variable var has spaces in it, again it will error out, see below.

Code:
$ a="Sat Aug 2 09:40:38 EDT 2008"                 # pattern found in the file  

$ awk  '/'$a'/' file
awk: /Sat
awk:  ^ unterminated regexp

# Whereas using awk's way of passing shell variables:

$ [COLOR=red]awk  -v var="$a" '$0 ~ var' file
Sat Aug 2 09:40:38 EDT 2008  xxxxxxxxxxxxxxxxxxxxx[/color]

So say if var has no spaces, then the code would have been something like this:
Code:
$ a=Sat
$ awk '/'$a'/' file
Sat Aug 2 09:40:38 EDT 2008  xxxxxxxxxxxxxxxxxxx
 
If var has space, simply quote it:
Code:
awk '/'[!]"[/!]$a[!]"[/!]'/{your awk program here}' file

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Aweosme i appreciate it. I think i get it now i really appreciate you taking the time to explain it. The new info will help out greatly.
 
arg well i still have one more problem while implementing this.... i cant get the shell to re-eval the variable.
i have 3 variables

get_schema=$( awk -v var="$1" '$0 ~ var{ print $2 }' databases.txt )
cmd_arg=2
schema_entries=$( awk -v var="$get_schema" -v var2="$cmd_arg" '$0 ~ var{ print $'var2' }' schemas.txt )

and a while loop

while [ "$schema_entries" != "" ]
do

echo "Please type a value for" $schema_entries
read answer
echo $schema_entries $answer >> $1.db
eval cmd_arg=`expr $cmd_arg + 1`

done

I dont know how to do your little code box or else i would srry...
ok the problem is this while loop doest change the value of $schema_entries
its never re-evaluated.
Sorry for all the questions like i said im just learning and awk has some weird rules
 
putting
schema_entries=$( awk -v var="$get_schema" -v var2="$cmd_arg" '$0 ~ var{ print $'var2' }'
at the bottom works but its hard to believe there isnt a command to reeval the variable without redefining it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top