Find the answer to your Linux question:
Results 1 to 2 of 2
Hi I need to use a shell variable inside a gawk expression, but i don't manage to do it the way i want to. To be more specific: Assume data.txt ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    18

    gawk and shell variables

    Hi

    I need to use a shell variable inside a gawk expression,
    but i don't manage to do it the way i want to.

    To be more specific:
    Assume data.txt is
    Code:
    200 312
    122 -53
    9 123
    44 0
    i now want to create a script file with the lines
    Code:
    yprocess 312 [some-complex-parameter-list] >> mod.txt
    yprocess -53 [some-complex-parameter-list] >> mod.txt
    yprocess 123 [some-complex-parameter-list] >> mod.txt
    yprocess 0 [some-complex-parameter-list] >> mod.txt

    so what i tried is:
    Code:
    a=$1
    b=calculate_complex_parameter_list  $a
    
    cat $a | gawk '{ print "yprocess " $2 " $b >> mod.txt" }'
    but this gives me
    Code:
    yprocess 312 $b >> mod.txt
    yprocess -53 $b >> mod.txt
    yprocess 123 $b>> mod.txt
    yprocess 0 $b >> mod.txt
    when i move the $b outside of the double quotes:
    Code:
    a=$1
    b=calculate_complex_parameter_list $a
    
    cat $a | gawk '{ print "yprocess " $2 " " $b " >> mod.txt"  }'
    i get
    Code:
    yprocess 312 200 312 >> mod.txt
    yprocess -53 122 -53 >> mod.txt
    yprocess 123 9 123 >> mod.txt
    yprocess 0 44 0 >> mod.txt
    i.e. $b is replaced by the current line emitted by cat

    In reality, my data file has several hundred lines,
    and the complex parameter list consists of several hunderd numbers.

    Is there any way to achieve this goal (with or without gawk=?

    Thank You
    jody

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    try something like below

    Code:
    #! /bin/sh
    
    echo -n "enter something->"
    read mymsg
    
    echo $mymsg | awk '{printf("%s\n", $0)}'
    Or...

    Code:
    #! /bin/sh
    
    echo -n "enter something->"
    read mymsg
    
    myval="123456789"
    
    myeval="echo $mymsg | awk '{printf(\"%s $myval\\n\", \$0);}'"
    
    eval $myeval
    Last edited by gerard4143; 03-18-2010 at 12:42 PM.
    Make mine Arch Linux

Posting Permissions

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