Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!sol.ctr.columbia.edu!ira.uka.de!fuchs From: fuchs@it.uka.de (Harald Fuchs) Newsgroups: comp.unix.shell Subject: Re: awk variables Message-ID: Date: 1 Dec 90 12:18:47 GMT References: <30404@boulder.Colorado.EDU> <8720009@hpdmd48.boi.hp.com> Sender: news@ira.uka.de (USENET News System) Organization: University of Karlsruhe, FRG Lines: 31 oscarh@hpdmd48.boi.hp.com (Oscar Herrera) writes: >>I am trying to use both shell variables and awk variables in the >>same awk call, but I cannot get both to work correctly. I have tried >>many variations of quoting and have had no success. The statement >>I want to use is : >> >> awk "NR == $line_num {print $1}" filename >> >>where $line_num is a script variable (an integer) and $1 is the >>awk variable for the first field. > try > awk 'NR == '$line_num '{print $1}' filename > Having $line_num not enclosed in single quotes should > allow the shell to get a hold of it and stick the variable value > in its place. This won't work. awk 'NR == '$line_num '{print $1}' filename ^ Here's the bug Awk expects its script to be the first argument, and when there's a blank after $line_num (or within an expanded shell variable like that) awk will see two arguments. Don't put blanks around $line_num and, if your shell variable might contain blanks, enclose it by double quotes: awk 'NR == '"$line_num"'{print $1}' filename -- Harald Fuchs ... *gulp*