Thanks to this great list... I can not imagine the response, This is an excellent list. which helped me solve my shell problem. The Question was: I want to parse some variable from shell to awk ( without using export ). for ex. if $1 & $2 are 2 arg's, then i want to use it in awk statement like awk '{ if ( ($SHELL_Variable1 < $1) && ($SHELL_Variable2 > $2) ) print "something" }' I tried with $ ' "${ SHELL_Variable1} " ' in all of the above forms; but its not working. - * -- * --- * ----- Many Thanks to the following experts for their nice Answers: Andrew J. Caines Johan Hartzenberg Matthew Hannigan Fabrice Guerini Lumpkin, Buddy Eric van de Meerakker Kacem El Kassemi Martin Steudten Thomas elline Glass, David (UDB) Hindley Nick Wanke Matthias Claude Charest Reggie Beavers ;most of them pointed out the quoting problem & also suggested to using awk/perl. ;Here are some of the Excellent explanations: ----------------------------------- Andrew J. Caines Wrote: > I want to parse some variable from shell to awk ( without using export ). Exporting only affects the scope of the variables within shell subprocesses and is not relevant to programs which manage their own variables. This is a simple issue of quoting. Singles quotes - '' - protect everything in them from shell expansion and special character handling and double quotes - "" - protect everything in them from special character handling but allow shell expansion (at least in simple terms). So if your shell has $1 and $2 defined and you want to use them in an in-line awk script, then you simply don't protect them from the shell, eg. # awk '{ if ( ('$1' < $1) && '$2' > $2) ) print "something" }' ---------- ------------ ------------------------------ The underlined sections are protected (ie. quoted), the rest is not and is therefore liable to shell expansion. You could also use double quotes and escape special characters, eg. something like.. # awk "{ if ( ($1' < \$1) && $2 > \$2) ) print \"something\" }" If you were actually trying to do a simple numerical comparison like the example, then you should use the shell's built-in test command, ie. # [ $1 -lt $abc1 -a $2 -gt $abc2 ] && echo "something" ..where you are populating $abc1 and $abc2 accordingly. See "Quoting" section of sh(1) and ksh(1) and test(1). --------------------------------------- Fabrice Guerini: $ min=6; max=100 $ cat > /tmp/x 1 2 5 7 12 7 762 1 63 76 19 276 0 5 992 2 8 43 $ awk '{ if (('$min' < $1) && ($2 < '$max')) { print $0 } }' /tmp/x 12 7 762 1 63 76 992 2 8 43 Cheers! --------------------------- Matthew Hannigan: It's painful isn't it! I'd suggest you use perl instead, which is supplied with Solaris these days but here is your answer: end the quotes and restart them just after the variable: awk '{ if (('$SHELL_Variable1' < $1) && ('$SHELL_Variable2' > $2)) print "something" }' or, just in case the shell variable results have spaces in them, use double quotes as well: awk '{ if (('"$SHELL_Variable1"' < $1) && ('"$SHELL_Variable2"' > $2)) print "something" }' --------------------------------- Eric van de Meerakker : It isn't working because the awk script itself is between single quotes (as it must be, or you won't be able to use the awk variables $1, $2, etc.), which prevents the substition of shell variables in the awk script text. The solution to your problem is to the -v option of /usr/xpg4/bin/awk or nawk, as follows: nawk -v SHELL_Variable1=$1 SHELL_Variable2=$2 '{ \ if ( (SHELL_Variable1 < $1) && (SHELL_Variable2 > $2) ) print "something" }' The -v option isn't availiable in /usr/bin/awk, but it will work for /usr/xpg4/bin/awk and /usr/bin/nawk (xpg4/bin/nawk and nawk are implementations of 'New' AWK, which has more features than 'classic' AWK, but also a few differences); see the man pages for awk and nawk. ------------------------------------ Hi, to the shell variables you have to quote them in 2 manners: '$var' (single quotes) or 4"$var"4 (one single quote + a double quote) ex. umask and SHLVL are 2 shell variables #!/bin/sh var1=25 var2=5 awk 'BEGIN { { if (('$umask' < '$var1' ) && ('"$SHLVL"' < '$var2')) \ {print "something " '$umask'" " '"$SHLVL"'} else {print "not ok"} }; exit }' Kacem El Kassemi ------------------------------------------ Your Support is Apreciated. Thanks, Abh, - - - - - - - - - - - - - - - Unix Administrator,KSA. Cell +96657255468 ------ _______________________________________________ sunmanagers mailing list sunmanagers@sunmanagers.org http://www.sunmanagers.org/mailman/listinfo/sunmanagersReceived on Wed Aug 28 04:35:23 2002
This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:42:53 EST