Unix AWK
DBAsupport.com Forums - Powered by vBulletin
Results 1 to 5 of 5

Thread: Unix AWK

  1. #1
    Join Date
    Jun 2002
    Posts
    64
    Hi All,

    Is it possible to get the value of a variable inside awk, which has a value assigned outside of the awk command.

    Example:

    test.ksh
    ----------

    choice=1
    export choice
    awk '{..........}' infile

    I want to get the value of choice inside the awk. Is it possible?


    Any help is appreciated.


    Thaks
    Najmal

  2. #2
    Join Date
    Sep 2002
    Posts
    42
    yes it is possible

    use $choice

    The script works
    In GOD I find everything

  3. #3
    Join Date
    Jun 2002
    Posts
    64

    Unix AWK

    Hi,

    It is not working that way.

    If I give,

    choice=1
    export choice
    awk '{print $choice}' infile

    It doesn't print anything.


    Please clarify how can it be done.

    Thanks for you resp.
    Najmal

  4. #4
    Join Date
    Sep 2002
    Posts
    42
    This is slightly complicated
    .you can try this code


    export choice=1
    awk '{
    for ( x in ENVIRON)
    if ( x ~ /choice/ )
    print ENVIRON[x]}
    ' infile

    This is more complicated than I thought

    GOOD LUCK
    In GOD I find everything

  5. #5
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    342
    use can use the -v option when starting awk :

    eg.
    year=$(date +%Y)
    echo test | awk -v yearstring=$year '{ print "xxx" yearstring "xxx" $1 }'

    Hope this helps
    Gert

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width