: Passing Variable to awk 
Author Message
 : Passing Variable to awk

Hi everyone,

    I'm a newbie in either awk and unix. I wonder if you guys can help me
out. I want to know whether or not your can build the awk program with the
combination of variable. I'm currently making a ksh script and I need to use
awk to filter some filename. OK here is what I got in mine.

I have a textfile and I wish to filter the word /^From:/ using awk from Line
10 to Line 29 and display them to the standard output (or rather put in a
variable and use it later)
so what I have is
    myVar=`awk 'NR > 10 && NR < 29 && /^From:/ {print $0;}' texfile`
it works fine, but I need some extension, because I want to change the line
number easily, such that
    start=10
    stop=29
    myVar=`awk 'NR > $start && NR < $start && /^From:/ {print $0;}' texfile`
but it won't work, I guess it's because of the single quotes. I tried puting
everything in a variable and do myVar=`$AWK_COMMAND`, it won't work as
well.. even though when I echoed $AWK_COMMAND, it displays the right things.

Thank you in advance,
Endra



Fri, 27 Sep 2002 03:00:00 GMT  
 : Passing Variable to awk

Quote:

>Hi everyone,

>    I'm a newbie in either awk and unix. I wonder if you guys can help me
>out. I want to know whether or not your can build the awk program with the
>combination of variable. I'm currently making a ksh script and I need to use
>awk to filter some filename. OK here is what I got in mine.

>I have a textfile and I wish to filter the word /^From:/ using awk from Line
>10 to Line 29 and display them to the standard output (or rather put in a
>variable and use it later)
>so what I have is
>    myVar=`awk 'NR > 10 && NR < 29 && /^From:/ {print $0;}' texfile`
>it works fine, but I need some extension, because I want to change the line
>number easily, such that
>    start=10
>    stop=29
>    myVar=`awk 'NR > $start && NR < $start && /^From:/ {print $0;}' texfile`
>but it won't work, I guess it's because of the single quotes. I tried puting
>everything in a variable and do myVar=`$AWK_COMMAND`, it won't work as
>well.. even though when I echoed $AWK_COMMAND, it displays the right things.

>Thank you in advance,
>Endra

try this:

start=10
stop=29
myVar=`awk -v var1=$start -v var2=$stop 'NR > var1 && NR < var2
       && /^From:/ {print $0;}' texfile`

or

start=10
stop=29
myVar=`awk 'NR > '"$start"' && NR < '"$stop"'
       && /^From:/ {print $0;}' texfile`

or

start=10
stop=29
myVar=`awk "NR > $start && NR < $stop && /^From:/ {print \$0}" texfile`

I haven't tested any of these, but they should all work.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 28 Sep 2002 03:00:00 GMT  
 : Passing Variable to awk
Thaaankss Charles... It works.. I owe you one..


Quote:


> >Hi everyone,

> >    I'm a newbie in either awk and unix. I wonder if you guys can help me
> >out. I want to know whether or not your can build the awk program with
the
> >combination of variable. I'm currently making a ksh script and I need to
use
> >awk to filter some filename. OK here is what I got in mine.

> >I have a textfile and I wish to filter the word /^From:/ using awk from
Line
> >10 to Line 29 and display them to the standard output (or rather put in a
> >variable and use it later)
> >so what I have is
> >    myVar=`awk 'NR > 10 && NR < 29 && /^From:/ {print $0;}' texfile`
> >it works fine, but I need some extension, because I want to change the
line
> >number easily, such that
> >    start=10
> >    stop=29
> >    myVar=`awk 'NR > $start && NR < $start && /^From:/ {print $0;}'
texfile`
> >but it won't work, I guess it's because of the single quotes. I tried
puting
> >everything in a variable and do myVar=`$AWK_COMMAND`, it won't work as
> >well.. even though when I echoed $AWK_COMMAND, it displays the right
things.

> >Thank you in advance,
> >Endra

> try this:

> start=10
> stop=29
> myVar=`awk -v var1=$start -v var2=$stop 'NR > var1 && NR < var2
>        && /^From:/ {print $0;}' texfile`

> or

> start=10
> stop=29
> myVar=`awk 'NR > '"$start"' && NR < '"$stop"'
>        && /^From:/ {print $0;}' texfile`

> or

> start=10
> stop=29
> myVar=`awk "NR > $start && NR < $stop && /^From:/ {print \$0}" texfile`

> I haven't tested any of these, but they should all work.

> Chuck Demas
> Needham, Mass.

> --
>   Eat Healthy    |   _ _   | Nothing would be done at all,

>   Die Anyway     |    v    | That no one could find fault with it.




Sat, 28 Sep 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. passing variables to awk scripts

2. Passing variables into awk.

3. Passing command line variables to AWK in shell script

4. How to pass shell variable to awk?

5. Passing the current directory path to a variable - awk on a PC

6. search on variable passed to awk /nawk

7. Passing an environment variable to a system call within awk

8. pass a variable from awk to shell

9. Pass an AWK variable to a pipeline program as a parameter

10. passing shell variable into awk program

11. how do i pass shell variables into awk?

12. passing shell variable containing many fields to awk

 

 
Powered by phpBB® Forum Software