Assigning a field variable in awk program to a shell variable - awk

This is a discussion on Assigning a field variable in awk program to a shell variable - awk ; hi myself a newbie to unix shell scripting and stuff.. i want to assign a field variable in an awk program to a shell variable. like for eg if shell_var is one of a shell variable then i want to ...

+ Reply to Thread
Results 1 to 3 of 3

Assigning a field variable in awk program to a shell variable

  1. Default Assigning a field variable in awk program to a shell variable

    hi
    myself a newbie to unix shell scripting and stuff..
    i want to assign a field variable in an awk program to a shell
    variable.
    like for eg
    if shell_var is one of a shell variable
    then i want to assign the line numbers returned by the grep command
    with -n option
    to a shell variable.

    the code that i've tried is this
    grep -n string filename | awk -F : '{shell_var = $1}'

    it doesn't work

    please help..


  2. Default Re: Assigning a field variable in awk program to a shell variable

    deeplights@ wrote:
    > hi
    > myself a newbie to unix shell scripting and stuff..
    > i want to assign a field variable in an awk program to a shell
    > variable.
    > like for eg
    > if shell_var is one of a shell variable
    > then i want to assign the line numbers returned by the grep command
    > with -n option
    > to a shell variable.
    >
    > the code that i've tried is this
    > grep -n string filename | awk -F : '{shell_var = $1}'


    First, you don't need grep if you use awk. The above can be written as

    awk '/string/ {shell_var = NR}' filename

    but here 'shell_var' is an awk variable.

    >
    > it doesn't work


    Now to make the above work

    shell_var=$( awk '/string/{print NR; exit}' filename )

    I added 'exit' because you just want a single line number in the shell
    variable.

    Janis


    >
    > please help..
    >


  3. Default Re: Assigning a field variable in awk program to a shell variable

    Janis Papanagnou wrote:
    > deeplights@ wrote:
    >
    >> hi
    >> myself a newbie to unix shell scripting and stuff..
    >> i want to assign a field variable in an awk program to a shell
    >> variable.
    >> like for eg
    >> if shell_var is one of a shell variable
    >> then i want to assign the line numbers returned by the grep command
    >> with -n option
    >> to a shell variable.
    >>
    >> the code that i've tried is this
    >> grep -n string filename | awk -F : '{shell_var = $1}'

    >
    >
    > First, you don't need grep if you use awk. The above can be written as
    >
    > awk '/string/ {shell_var = NR}' filename
    >
    > but here 'shell_var' is an awk variable.
    >
    >>
    >> it doesn't work

    >
    >
    > Now to make the above work
    >
    > shell_var=$( awk '/string/{print NR; exit}' filename )
    >
    > I added 'exit' because you just want a single line number in the shell
    > variable.
    >
    > Janis


    And if you want to pass the value of a shell variable to an awk script
    in future, see question 24 in the comp.unix.shell FAQ
    (http://home.comcast.net/~j.p.h/cus-faq-2.html#24).

    Ed.

+ Reply to Thread

Similar Threads

  1. Assigning a reference to a variable
    By Application Development in forum CSharp
    Replies: 23
    Last Post: 12-17-2007, 05:02 AM
  2. Replies: 1
    Last Post: 06-28-2007, 10:03 AM
  3. Assigning address of a variable in VC#.NET to a variable in VC++.N
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 05-16-2007, 07:57 PM
  4. assigning a string variable to a memo field.
    By Application Development in forum Clipper
    Replies: 1
    Last Post: 12-26-2005, 05:32 PM
  5. How To Dim a Variable When Assigning a Recordset to the Variable
    By Application Development in forum basic.visual
    Replies: 3
    Last Post: 12-09-2003, 11:37 AM