Reading VAR 
Author Message
 Reading VAR

Hello,

    I'm having problem passing variable to awk within
a shell script

echo "Enter string.."
read string
awk 'BEGIN {FS=":";RS=""} $1==string {print$0}' /tmp/tmp.$$

How can i passwd this variable to awk?. Or can I at all?
I was thinking of doing
echo $string > /tmp/string.tmp. Then read /tmp/string.tmp
into a var within awk. But i'm having trouble with that
also.
Thanks in advance.

Tim.



Fri, 03 Mar 2000 03:00:00 GMT  
 Reading VAR

Quote:

>Hello,

>    I'm having problem passing variable to awk within
>a shell script

>echo "Enter string.."
>read string
>awk 'BEGIN {FS=":";RS=""} $1==string {print$0}' /tmp/tmp.$$

>How can i passwd this variable to awk?. Or can I at all?
>I was thinking of doing
>echo $string > /tmp/string.tmp. Then read /tmp/string.tmp
>into a var within awk. But i'm having trouble with that
>also.
>Thanks in advance.

Tim --

This can be solved fairly portably using quoting tricks.

# ------- cut here -------
#!/bin/sh

echo "Enter string: \c"
read string

nawk '
BEGIN {

   FS = ":"
   RS = ":"

   AwkString = "'"$string"'"   # quote trick is here
                               # outer double quotes are for awk
                               # middle single quotes are for sh
                               # inner double quotes make sure multi
                               #  word $string comes thru to awk

Quote:
}

{
   if ( $1 == AwkString )
      print

Quote:
}' /tmp/tmp.$$

exit 0

# ------- cut here -------

-- kjh

--
------------------------------------------------------------

1111 Seacoast Dr.  Unit 41   |  home:   (619) 423-4451     |
Imperial Beach, CA   91932   |                             |



Fri, 03 Mar 2000 03:00:00 GMT  
 Reading VAR

Quote:

> Hello,

>     I'm having problem passing variable to awk within
> a shell script

> echo "Enter string.."
> read string
> awk 'BEGIN {FS=":";RS=""} $1==string {print$0}' /tmp/tmp.$$

> How can i passwd this variable to awk?. Or can I at all?
> I was thinking of doing
> echo $string > /tmp/string.tmp. Then read /tmp/string.tmp
> into a var within awk. But i'm having trouble with that
> also.
> Thanks in advance.

> Tim.

The easiest way is to use the -v option, see below

envvar=Tim
echo Hello |awk -vvariable=$envvar '{print $1 " " variable}'

Hope this helps

Al
--

CSC Ra Group Ltd.
In this posting/mail I am speaking for me, not my employer



Fri, 03 Mar 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. HELP - Problems reading var. len. in COBOL II

2. Expect question reading more than one var from same line from file

3. var substitution while reading a file

4. awk: -v Var=VAR Obviously not a syntax issue

5. update Var if Var==nil

6. To VAR or not to VAR...

7. list[] = var => list.append(var) (phpstyle)

8. tclperl - how to pass tcl var to perl var

9. var window size and var # of data

10. What different between Instance var, method & Class var,method?

11. What different between Instance var, method & Class var,method?

12. Zope: What difference between "&dtml-var" and "&dtml.-var"

 

 
Powered by phpBB® Forum Software