Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2003

    Unanswered: programfile & flags/variables as a variable

    Ok I'm stuck....

    I've got a variable called $DATA that basically contains:
    NR==1 {print $0} NR==2 {print "inserted data" substr($0,96)}.....

    What I want to do is use this as part of the awk 'command line'

    ie: awk '$DATA' file_being_read.dat

    No matter what I do I can't seem to make this work...Is it possible? I just want to string together the 'command line' for awk and run it...


  2. #2
    Join Date
    Jun 2002
    You'll need to use 'eval' to evaluate the line before passing it to the shell. You'll need to make some slight changes though to escape items in your awk string that don't want to be evaluated initially.

    DATA="NR==1 {print \$0} NR==2 {print \"inserted data\" substr(\$0,96)}"

    eval awk '$DATA' file_being_read.dat

    I always use 'echo' to test the string that I will be evaluating (as this will itself evaluate the input before echoing it out)...

    $ echo $DATA
    $ NR==1 {print $0} NR==2 {print "inserted data" substr($0,96)}


Posting Permissions

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