gs@diab.se (Greger Sernemar) (09/27/89)
clewis@eci386.uucp (Chris Lewis) writes: >In article <1163@ispi.UUCP> jbayer@ispi.UUCP (Jonathan Bayer) writes: >> >>HELP!! I have been pulling my hair out over this seemingly simple >>problem: >>a=" awk -F: '\$1 == \"$LOGNAME\" { >> user=\$5; >> print user >> }'" >>USER=`cat /etc/passwd | $a` >>I get the following error: >Various ugly syntax errors. Here is a solution to get parameters into an awk program: a="`awk -F: '$1 == NAME { user=$5; print user }' NAME=$LOGNAME - `" The last dash (-) closes standard input i.e enables other program to pipe to the awk script. I was forced to either supply a file name or a dash to be able to set the awk variable. I'm not sure if this is a bug in the version of awk I'm using or not. -- Greger Sernemar, Diab Data AB SNAIL: Box 2029, S-183 02 Taby, Sweden ANALOG: +46 8-7680660 EMAIL: gs@diab.se , mcsun!sunic!diab!gs
andyb@coat.com (Andy Behrens) (09/28/89)
In article <488@diab.se> gs@diab.se (Greger Sernemar) writes: > Here is a solution to get parameters into an awk program: > > a="`awk -F: '$1 == NAME { > user=$5; > print user > }' NAME=$LOGNAME - `" > > The last dash (-) closes standard input i.e enables other program to > pipe to the awk script. I was forced to either supply a file name or > a dash to be able to set the awk variable. > I'm not sure if this is a bug in the version of awk I'm using or not. I'm not sure whether it's a bug or a feature, but it exists on all versions of awk that I've ever seen. Variable is not available for use within the script until after the first record has been read and parsed, but it is available as soon as that has occurred so that it may be used before any other processing begins. It does not exist at the time the BEGIN block is executed, and if there was no input it will not exist in the END block (if any). The variable assignment is not made until the filename is encountered in the argument list, but (feature!) you can reassign values between files. For example: #!/bin/sh # Display user names and their default groups sort /etc/passwd | awk -F: 'file=="G" { groupname[$3] = $1 } file=="P" { printf "%-12s%s\n", $1, groupname[$4] }' file=G /etc/group file=P -