[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Importing Shell Variables in to awk from Shell Scripts



On Tue, Apr 05, 2005 at 10:04:20AM -0500, Martin McCormick wrote:
> 	I have a shell script that is supposed to search a file of
> text and, if the sixth field in a line matches a search string, print
> the whole line.  This seems like a good job for awk and, after some
> searching, I found that you can import variables in to awk something
> like:
> 
> awk '(/'"$searchvar"'/ == $6) {print $0}' "$inputfile"
> 
> 	The problem is while the awk script runs, the results are
> either the entire text file if I use a begin block
> 
> awk 'begin {(/'"$searchvar"'/ == $6)} {print $0}' "$inputfile"
> 
> 	Or it is truncated if the first example is used and one gets a
> large portion but not the entire text file spewed out.  In neither
> case does any testing appear to be happening to see if $searchvar
> matches $6.
> 
> 	If the parentheses are removed, you get the whole input file
> sent to the output, again with no testing.
> 
> 	Thanks for any ideas on other syntax to try.  Some would do
> this in perl, but awk should do this job.
> Martin McCormick WB5AGZ  Stillwater, OK 
> OSU Information Technology Division Network Operations Group
> 

I use gawk, so I'm not sure if this suggestion will work in other
implementations of awk:

The -v option allows one to pass a shell variable value into an awk
script. The syntax is -v var=val where var is a variable name in the
awk script and val is a shell expression that returns a value. 
See man gawk.

-- 
Paul E Condon           
pecondon@mesanetworks.net



Reply to: